Last active
January 11, 2022 01:14
-
-
Save franzwong/3817585 to your computer and use it in GitHub Desktop.
Resize image with Python
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 traceback | |
from PIL import Image | |
def resize(): | |
filePath = 'example.jpg' | |
ratio = 0.5 | |
image = Image.open(filePath) | |
width = image.size[0] | |
height = image.size[1] | |
newWidth = int(round(width * ratio)) | |
newHeight = int(round(height * ratio)) | |
newImage = image.resize((newWidth, newHeight), Image.ANTIALIAS) | |
newImage.format = image.format | |
newImage.save(filePath) | |
if __name__ == '__main__': | |
try: | |
resize() | |
except Exception as e: | |
traceback.print_exc(e) | |
raw_input() |
@darkronin Thanks for the comment. PIL
was replaced by Pillow
. That causes the change on the module. I have updated that.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
super useful, thanks!
adding only: the import statements should become