快速使用PIL转换照片生成预览网页

我们常常用很多照片,需要快速通过http服务器分享,下面这个脚本,用PIL(python image library)转换固定大小的缩略图,并生成一个简易的html文件,附带的链接可以用于点击察看大图片。递归目录、其他格式什么懒的支持了,这个够用了 🙂

$ cat resize.py

import os, Image
# Resize jpg files in current directory, and generate an html file to 
# present pictures in a light way.
# $ cd picture-dir/
# $ python resize.py
# $ firefox index.html

def resize(filename, width = 512):
    img = Image.open(filename)
    size = img.size
    new = img.resize((width, width * size[1] / size[0]))
    new.save(".%s" % filename)

f = open("index.html", "w")
f.write("Click to review big pictures")
files = os.listdir(".")
files.sort()
for i in files:
    if (i[-4:]==".JPG" or i[-4:]==".jpg") and i[0:1]!='.':
        resize(i, 200)
        f.write("n" % (i, i))
        f.flush()
f.close()

/blog/wp-content/uploads/resize-pic-script.png

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.