刚开始学习Python,利用PIL完成一个批量图像格式转化的应用。
'''Created on 2013\08\10@author: xwolfs'''import osfrom PIL import Imagedef 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