Skip to content

Instantly share code, notes, and snippets.

@josix
Created August 5, 2022 08:11
Show Gist options
  • Select an option

  • Save josix/cda6268eddad15db980c3e4713053363 to your computer and use it in GitHub Desktop.

Select an option

Save josix/cda6268eddad15db980c3e4713053363 to your computer and use it in GitHub Desktop.
resizing all given image arguments in the CLI
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