Last active
April 12, 2021 22:20
-
-
Save ggorlen/e7cb3f58f034fa0d7de21fc21f3a7ae4 to your computer and use it in GitHub Desktop.
batch resize photos
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 os | |
| from PIL import Image | |
| def main(): | |
| # TODO https://stackoverflow.com/questions/4228530/pil-thumbnail-is-rotating-my-image | |
| in_dir = "." | |
| out_dir = "thumbs" | |
| max_dimensions = 350, 350 | |
| extension = ".jpg" | |
| postfix = "_thumb.jpg" | |
| if not os.path.isdir(out_dir): | |
| os.mkdir(out_dir) | |
| for item in os.listdir(in_dir): | |
| path = os.path.join(in_dir, item) | |
| if os.path.isfile(path) and item.endswith(extension): | |
| img = Image.open(path) | |
| fname, _ = os.path.splitext(path) | |
| img.thumbnail(max_dimensions, Image.ANTIALIAS) | |
| img.save(os.path.join(out_dir, fname + postfix), "JPEG", quality=90) | |
| if __name__ == "__main__": | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment