Skip to content

Instantly share code, notes, and snippets.

@thesaadarshad
Created January 26, 2020 19:22
Show Gist options
  • Save thesaadarshad/282341eb86efc9f9960254997af5faec to your computer and use it in GitHub Desktop.
Save thesaadarshad/282341eb86efc9f9960254997af5faec to your computer and use it in GitHub Desktop.
python image resize with ratio
#!/usr/bin/env python
import PIL
from PIL import Image
import os, sys
mywidth = 1200
path = "/pathtofolder/"
dirs = os.listdir( path )
def resize():
for item in dirs:
if os.path.isfile(path+item):
img = Image.open(path+item)
wpercent = (mywidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((mywidth,hsize), PIL.Image.ANTIALIAS)
img.save(path+'resized/'+item)
resize()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment