【发布时间】:2013-07-07 22:19:09
【问题描述】:
我有一个任意大小的源文件image.ico,并且想要创建一个缩略图。这是我现在使用的代码:
converted_file = cStringIO.StringIO()
thumb = ImageOps.fit(image, (width, height), Image.ANTIALIAS)
thumb.save(converted_file, format='png')
我选择 png 作为扩展名是因为 PIL 不支持可能是罪魁祸首的 ico 文件。除了不应用透明度这一事实之外,它还有效。 alpha=0 的部分被渲染为黑色而不是透明的。我该如何解决这个问题?
/编辑
我也试过了(see this answer):
converted_file = cStringIO.StringIO()
thumb = ImageOps.fit(image, (width, height), Image.ANTIALIAS)
background = Image.new('RGBA', (width, height), (255, 255, 255, 0))
background.paste(thumb, box = (0, 0, width, height))
background.save(converted_file, format='png')
同样的效果。
【问题讨论】:
-
你看图片怎么样?例如,尝试在 Chrome 中打开它。透明区域是否仍然显示为黑色?
-
是的。这个答案可能是解决方案:stackoverflow.com/questions/987916/…
标签: python python-imaging-library alpha