Skip to content

Instantly share code, notes, and snippets.

@sany2k8
Forked from franzwong/resize.py
Created July 18, 2021 07:55
Show Gist options
  • Save sany2k8/d358c5988f43e4c1eb25e9779d5bae88 to your computer and use it in GitHub Desktop.
Save sany2k8/d358c5988f43e4c1eb25e9779d5bae88 to your computer and use it in GitHub Desktop.
Resize image with Python
# Require PIL (Python Imaging Library)
import traceback, 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment