python生成缩略图

一、环境配置

确保系统安装了libjpeg libjpeg-dev libfreetype6 libfreetype6-dev zlib1g-dev

如果系统未安装,则会出现  JPEG support not available 的异常。 安装libjpeg-dev需要注意对安装的库文件生成链接,即执行:

# ln -s /usr/lib/i386-linux-gnu/libjpeg.so /usr/lib

# ln -s /usr/lib/i386-linux-gnu/libfreetype.so /usr/lib

# ln -s /usr/lib/i386-linux-gnu/libz.so /usr/lib

然后删掉 PIL 的解压文件,重新解压一份再安装即可。

二、使用PIL生成缩略图

#-*-!/usr/bin/env python-*-
#-*-coding=utf-8-*-
from PIL import Image
import os
import sys
import fnmatch
import logging

class ThumbnailGenerator:
    # --*-- exts 为规定的后缀名列表,空则获取所有类型文件 --*--
    def __getFileList(self, path, exts=None, subdir=True):
        if os.path.exists(path):
            dirlist = []
            for name in os.listdir(path):
                fullname = os.path.join(path, name)
                if os.path.isdir(fullname):
                    dirlist += self.getFileList(fullname,exts)
                else:
                    if exts and len(exts) > 0:
                        for ext in exts:
                            if fnmatch.fnmatch(fullname, ext):
                                dirlist.append(fullname)
                                break
                    elif exts == None or len(exts) == 0:
                        dirlist.append(fullname)                        
            return dirlist
        else:
            return []

    # --*-- sizes 参数传递要生成的尺寸,可以生成多种尺寸,每个size是一个二值元组 --*--
    def __make_thumb(self, source_file, dest_path, sizes):
        basename = os.path.basename(source_file)
        dest_path 
        try:
            im = Image.open(source_file)
        except IOError, e:
            logging.error('Open %s error: ' + str(e), source_file)
            return
        mode = im.mode
        if mode not in ('L', 'RGB'):
            if mode == 'RGBA':
                # 透明图片需要加白色底
                alpha = im.split()[3]
                bgmask = alpha.point(lambda x: 255-x)
                im = im.convert('RGB')
                # paste(color, box, mask)
                im.paste((255,255,255), None, bgmask)
            else:
                im = im.convert('RGB')                
        width, height = im.size
        if width == height:
            region = im
        else:
            if width > height:
                delta = (width - height)/2
                box = (delta, 0, delta+height, height)
            else:
                delta = (height - width)/2
                box = (0, delta, width, delta+width) 
            region = im.crop(box)                
        for size in sizes:
            filename = os.path.join(dest_path,basename)
            thumb = region.resize((size[0],size[1]), Image.ANTIALIAS)
            thumb.save(filename, quality=100) # 默认 JPEG 保存质量是 75, 不太清楚。可选值(0~100)

    def thumb_generate(self, source_path, dest_path, sizes, subdir=True):
        exts = ['*.jpg','*.png','*.JPG','*.PNG']
        file_list = self.__getFileList(source_path, exts, subdir)
        for file in file_list:
            try:
                self.__make_thumb(file, dest_path, sizes)
            except Exception, e:
                logging.error('Thumb %s error: ' + str(e), file)

if __name__ == '__main__':
    logging.basicConfig(filename='thumb.log',filemode='w',format='[%(asctime)s]-%(levelname)s : %(message)s',level=logging.DEBUG)

    sizes = [(128,96)]
    source_path = '201206'
    dest_path = 's'

    generator = ThumbnailGenerator()
    generator.thumb_generate(source_path,dest_path,sizes)


你可能感兴趣的:(python)