Question

I want to take a list of files from directory and convert all those images into thumbnail images. The following code can write a single file as thumnail.png. How can i write all images like "im.save("Thumbnail1.png","PNG") to thumnbnailN.png" . I want all files to be converted and written to a folder. How can i do that? Thanks in Advance.

from PIL import Image
from numpy import *
import os

def get_imlist(path):
    return [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.png')]

size=25,25
path='D:/'
filelist=get_imlist(path)
for file in filelist:
    im=(Image.open(file))
    im = im.resize(size, Image.ANTIALIAS)
    im.save("Thumbnail.png","PNG")
    imgArr = array(im)
    #print imgArr.shape
Was it helpful?

Solution

I'd replace your imlist stuff with glob. That way you can use something like

for file in glob.iglob('D:/path/*.png')

And you should look more into os.path. Specifically os.path.split and os.path.splitext.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top