fromPILimportImageimportos.pathimportStringIOdefthumbnail(filename,size=(50,50),output_filename=None):image=Image.open(filename)ifimage.modenotin('L','RGB'):image=image.convert('RGB')image=image.resize(size,Image.ANTIALIAS)# get the thumbnail data in memory.ifnotoutput_filename:output_filename=get_default_thumbnail_filename(filename)image.save(output_filename,image.format)returnoutput_filenamedefthumbnail_string(buf,size=(50,50)):f=StringIO.StringIO(buf)image=Image.open(f)ifimage.modenotin('L','RGB'):image=image.convert('RGB')image=image.resize(size,Image.ANTIALIAS)o=StringIO.StringIO()image.save(o,"JPEG")returno.getvalue()defget_default_thumbnail_filename(filename):path,ext=os.path.splitext(filename)returnpath+'.thumb.jpg'