Skip to content

Instantly share code, notes, and snippets.

@CnrLwlss
Last active December 15, 2015 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CnrLwlss/5330170 to your computer and use it in GitHub Desktop.
Save CnrLwlss/5330170 to your computer and use it in GitHub Desktop.
Find all images in the current directory and generate resized versions, together with a single preview file displaying all patched versions pasted together.
import PIL, os, math
from PIL import Image
resizeWidth=500
allfiles=os.listdir(os.getcwd())
filelist=[f for f in allfiles if f[-4:] in ['.jpg','.JPG','.jpeg','.JPEG','.png','.PNG']]
Nimages=len(filelist)
im0=Image.open(filelist[0])
w,h=im0.size
neww=resizeWidth
newh=int(round((float(neww)/float(w))*h))
bigim=Image.new("RGB",(2*neww,int(math.ceil(float(Nimages)/2.0))*newh))
row,col=0,0
for imname in filelist:
im=Image.open(imname)
small=im.resize((neww,newh),Image.ANTIALIAS)
small.save("small_"+imname[0:-4]+".png")
bigim.paste(small,(col*neww,row*newh,(col+1)*neww,(row+1)*newh))
print row,col
col=col+1
if col>1:
col=0
row=row+1
bigim.save("Preview.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment