Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save giig982/44200e602019769612e59267d6e25396 to your computer and use it in GitHub Desktop.

Select an option

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.
#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