Skip to content

Instantly share code, notes, and snippets.

@franzwong
Last active January 11, 2022 01:14
Show Gist options
  • Save franzwong/3817585 to your computer and use it in GitHub Desktop.
Save franzwong/3817585 to your computer and use it in GitHub Desktop.
Resize image with Python
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
Copy link

super useful, thanks!

adding only: the import statements should become

import traceback
from PIL import Image

@franzwong
Copy link
Author

@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