Created
August 5, 2022 08:11
-
-
Save josix/cda6268eddad15db980c3e4713053363 to your computer and use it in GitHub Desktop.
resizing all given image arguments in the CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import argparse | |
| from pathlib import Path | |
| from PIL import Image | |
| parser = argparse.ArgumentParser(description='Process some images...') | |
| parser.add_argument('image_paths', metavar='img_path', type=str, nargs='+', | |
| help='image path for resizing') | |
| args = parser.parse_args() | |
| OUTPUT_SIZE = (200, 200) | |
| for img_path in args.image_paths: | |
| img_path = Path(img_path) | |
| old_stem, old_ext = img_path.stem, img_path.suffix | |
| directory = img_path.parent | |
| new_path = Path(directory, f"resized-{old_stem}{old_ext}") | |
| image = Image.open(img_path).copy() | |
| print(f"Processing: {img_path=} {image.size=} -> {OUTPUT_SIZE=}") | |
| image.thumbnail(OUTPUT_SIZE) | |
| print(f"Saved to {new_path}") | |
| image.save(new_path, "JPEG") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment