9

As the title says i am looking for a way convert a huge number of images into thumbnails of different sizes , How do i go about doing this in python

RC1140
  • 8,423
  • 14
  • 48
  • 71

1 Answers1

22

See: http://www.pythonware.com/products/pil/index.htm

import os, sys
import Image

size = 128, 128

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size)
            im.save(outfile, "JPEG")
        except IOError:
            print "cannot create thumbnail for", infile
Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232
  • 2
    If you need it to be a square thumbnail regardless of the original image's aspect ratio, then see http://stackoverflow.com/questions/1386352/pil-thumbnail-and-end-up-with-a-square-image. – Seth Mar 13 '14 at 16:41
  • 2
    **Update (2020)**: `from PIL import Image`, see https://www.tutorialspoint.com/python_pillow/python_pillow_creating_thumbnails.htm – fralau Dec 26 '20 at 10:03