Python 图像格式转换

刚开始学习Python,利用PIL完成一个批量图像格式转化的应用。

'''
Created on 2013810

@author: xwolfs
'''
import os
from PIL import Image

def get_imlist(path,fmt_input):
    """ Return a list of filenames for all bmp images in a directory."""
    return [os.path.join(path,f) for f in os.listdir(path) if f.endswith(fmt_input)]

def convert_image_fmt(path, fmt_input,fmt_output):
    im_list = get_imlist(path,fmt_input)
    for infile in im_list:
        outfile=os.path.splitext(infile)[0]+fmt_output
        if infile != outfile:
            try:
                Image.open(infile).save(outfile)
            except IOError:
                    print "cannot convert", infile
原文地址:https://www.cnblogs.com/xwolfs/p/3249883.html