使用python为jpegs创建缩略图

RC1*_*140 5 python image-processing

正如标题所说,我正在寻找一种方法将大量图像转换为不同大小的缩略图,我如何在python中执行此操作

Kev*_*tre 17

请参阅: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
Run Code Online (Sandbox Code Playgroud)

  • 如果无论原始图像的长宽比如何,都需要将其作为正方形缩略图,请参阅http://stackoverflow.com/questions/1386352/pil-thumbnail-and-end-up-with-a-square-image。 (2认同)
  • **更新(2020)**:`从 PIL 导入图像`,请参阅 https://www.tutorialspoint.com/python_pillow/python_pillow_creating_thumbnails.htm (2认同)