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\0'
- # recursively resize images with python
- for image in os.listdir('.'):
- fp = 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] < 3000) or (im.size[1] < 3000):
- #find the coeffecient to scale smallest edge to 550
- width = 3000 / float(im.size[0])
- height = 3000 / float(im.size[1])
- ratio = min(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:
- import traceback;print(traceback.format_exc())
- print(e)
Advertisement
Add Comment
Please, Sign In to add comment