Forked from tomvon/resize-image-keep-aspect-ratio.py
Last active
March 7, 2022 15:01
-
-
Save giig982/44200e602019769612e59267d6e25396 to your computer and use it in GitHub Desktop.
Python script to resize an image if bigger than maxWidth, while keeping the original aspect ratio.
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
| #Resizes an image and keeps aspect ratio. Set mywidth to the desired with in pixels. | |
| import PIL | |
| from PIL import Image | |
| def resizeImageIfNeeded(file_path): | |
| maxWidth = 3000 | |
| img = Image.open(file_path) | |
| imgWidth = img.size[0] | |
| imgHeight = img.size[1] | |
| if imgWidth > maxWidth: | |
| wpercent = (maxWidth/float(imgWidth)) | |
| hsize = int((float(imgHeight)*float(wpercent))) | |
| img = img.resize((maxWidth,hsize), Image.ANTIALIAS) | |
| img.save(file_path) | |
| return True | |
| else: | |
| return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment