Advertisement
Guest User

Untitled

a guest
May 29th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. import os,sys
  2. import Image
  3.  
  4. size = 256, 256
  5.  
  6. for (path, dirname,files) in os.walk(sys.argv[1]):
  7. for f in files:
  8. ext = os.path.splitext(f)
  9. upper_ext = ext[1].upper()
  10. outfile = os.path.join(path, '/root/output', ext[0] + '_256x256' + upper_ext)
  11. try:
  12. new_img = Image.new("RGB", (256,256), "white")
  13. im = Image.open(os.path.join(path,f))
  14. im.thumbnail(size, Image.ANTIALIAS)
  15. load_img = im.load()
  16. load_newimg = new_img.load()
  17. i_offset = (256 - im.size[0]) / 2
  18. j_offset = (256 - im.size[1]) / 2
  19. for i in range(0, im.size[0]):
  20. for j in range(0, im.size[1]):
  21. load_newimg[i + i_offset,j + j_offset] = load_img[i,j]
  22.  
  23. if(upper_ext == '.JPEG' or upper_ext == '.JPG'):
  24. new_img.save(outfile, "JPEG")
  25. elif(upper_ext == '.PNG'):
  26. new_img.save(outfile, "PNG")
  27. except IOError:
  28. print "cannot create thumbnail for '%s'" % os.path.join(path, f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement