How do I resize picture files with PIL?

Asked 2 years ago, Updated 2 years ago, 87 views

You want to resize the thumbnail while maintaining the original proportion in the plot.

How can I change the size of the thumbnail in PIL?

image python pil python-imaging-libaray thumbnail

2022-09-22 22:14

1 Answers

If it's a thumbnail, I guess it's to reduce the size.

Set the maximum size and obtain the ratio with ratio = min(maxwidth/width, maxheight/height).

Or you can use Image.thumbnail(size, resample=3) provided by PIL.

(size) Create a thumbnail below.)

Please refer to the example in the tutorial

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, Image.ANTIALIAS)
            im.save(outfile, "JPEG")
        except IOError:
            print "cannot create thumbnail for '%s'" % infile


2022-09-22 22:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.