Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import imghdr, os
- from PIL import Image
- directory = 'C:/Users/UserBob/Desktop/RANDOM/pages'
- # recursively resize images with python
- for root, dirs, files in os.walk(directory):
- for image in files:
- fp = os.path.join(root,image)
- try:
- # if file is image
- if imghdr.what(fp) is not None:
- with open('fn','a+') as f:
- f.write(fp+'\n')
- im = Image.open(fp)
- if (im.size[0] < 500) or (im.size[1] < 500):
- #find the coeffecient to scale smallest edge to 550
- width = 550 / float(im.size[0])
- height = 550 / float(im.size[1])
- ratio = max(width, height)
- width = int(float(im.size[0]) * ratio)
- height = int(float(im.size[1]) * ratio)
- #NEAREST for upsizing, antialias for downsizing
- resized_image = im.resize((width, height), Image.NEAREST)
- resized_image.save(fp, format='JPEG')
- except Exception, e:
- print(e)
Advertisement
Add Comment
Please, Sign In to add comment