Thursday, May 10, 2007

用python来缩放图片

Python Imaging Library (PIL) 是一个普遍使用的图片库,你需要下载安装。这里有一段示例代码,最新的代码看这里

#!/usr/bin/env python
# $Id: thumbnails_create.py 5 2007-05-10 05:44:05Z zhuzhu $
# first you must install Python Imaging Library (PIL) at
# http://www.pythonware.com/products/pil/
import os, sys
import Image
import glob

size = [580,1024]
files = glob.glob('*.jpg')
# files = ["Leah_Dizon_613516.jpg"]

for infile in files:
im = Image.open(infile)
if im.size[0] > size[0]:
how = (size[0]*100)/im.size[0]
outfile = os.path.splitext(infile)[0] + ".webcan.jpg"
if infile != outfile:
try:
im = im.resize((size[0], (im.size[1]*how)/100), Image.BILINEAR)
im.save(outfile, "JPEG")
print outfile+" is created"
except IOError:
print "cannot create thumbnail for", infile
else:
print "not need create thumbnail for", infile


我还收集了一个python的小小手册,相当于是FAQ吧。

No comments:

Post a Comment