From 2e492734484d7532178bf23e810be78730a03061 Mon Sep 17 00:00:00 2001 From: pi Date: Tue, 14 Jan 2020 14:59:03 +0900 Subject: [PATCH] first commit --- .gitignore | 3 ++ Pipfile | 15 ++++++++ README.md | 14 +++++++ image-resize.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 .gitignore create mode 100644 Pipfile create mode 100755 image-resize.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fbd7418 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.venv/ +.vscode/ +Pipfile.lock diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..3796c78 --- /dev/null +++ b/Pipfile @@ -0,0 +1,15 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] +pylint = "*" +flake8 = "*" +autopep8 = "*" + +[packages] +pillow = "*" + +[requires] +python_version = "3.8" diff --git a/README.md b/README.md index e8bb58e..6966187 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,16 @@ # image-resize Change size of image file + +## Usage +usage: image-resize [-h] (--width pix | --height pix) [--up] imagefile + +Resize image file. + +positional arguments: + imagefile image file name + +optional arguments: + -h, --help show this help message and exit + --width pix, -w pix witdh(pixel) + --height pix, -H pix height(pixel) + --up, -u accept scale up \ No newline at end of file diff --git a/image-resize.py b/image-resize.py new file mode 100755 index 0000000..c21ce10 --- /dev/null +++ b/image-resize.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python + +import sys + + +def resize(args): + from PIL import Image + + try: # 画像ファイルを読み込む + im = Image.open(args.imagefile) + except IOError: + print('Error: image file not found.', file=sys.stderr) + return 1 + + try: + oriantation = get_exif(im)['Orientation'] + except KeyError: + oriantation = 1 + + org_width, org_height = im.size + res_width, res_height = (0, 0) + + if args.width and args.width > 0: + if args.width == org_width: + resize = False + elif args.up: + resize = True + elif args.width < org_width: + resize = True + else: + print( + f'Error: image width is less than {args.width} pixels.', + file=sys.stderr) + return 1 + if resize: + res_width = args.width + res_height = (int)(org_height * args.width / org_width) + + elif args.height and args.height > 0: + if args.height == org_height: + resize = False + elif args.up: + resize = True + elif args.height < org_height: + resize = True + else: + resize = False + print( + f'Error: image height is less than {args.height} pixels.', + file=sys.stderr) + return 1 + if resize: + res_width = (int)(org_width * args.height / org_height) + res_height = args.height + + else: + print(f'Error: [width | height] shuld be > 0', file=sys.stderr) + return 1 + + if resize: + res_im = im.resize((res_width, res_height)) + if oriantation > 1: + trans = (0, 0, 0, 3, 1, 5, 4, 6, 2) + res_im = res_im.transpose(trans[oriantation]) + res_im.save(args.imagefile) + + return 0 + + +def get_exif(im): + from PIL.ExifTags import TAGS + + exif = im._getexif() + exif_table = {} + if exif: + for tag_id, value in exif.items(): + tag = TAGS.get(tag_id, tag_id) + exif_table[tag] = value + + return exif_table + + +if __name__ == '__main__': + import argparse as ap + + parser = ap.ArgumentParser( + prog='image-resize', description='Resize image file.') + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument('--width', '-w', metavar='pix', + type=int, help='witdh(pixel)') + group.add_argument('--height', '-H', metavar='pix', + type=int, help='height(pixel)') + parser.add_argument('--up', '-u', action='store_true', + help='accept scale up') + parser.add_argument('imagefile', type=str, help='image file name') + args = parser.parse_args() + + res = resize(args) + sys.exit(res)