如何用PIL确定ICO图像的透明颜色指数?
具体来说,这是来自 .ico 文件,因此不存在像 gif 中那样的“透明”“信息”属性。 下面的示例说明了使用正确的透明度索引“0”将 Yahoo! 的图标转换为 png,这是我猜测的。 如何检测 ico 实际上是透明的并且透明度指数为 0 ?
import urllib2
import Image
import StringIO
resp = urllib2.urlopen("http://www.yahoo.com/favicon.ico")
image = Image.open(StringIO.StringIO(resp.read()))
f = file("test.png", "w")
# I guessed that the transparent index is 0. how to
# determine it correctly ?
image.save(f, "PNG", quality=95, transparency=0)
Specifically, this is from an .ico file, so there is no "transparent" "info" attribute like you would get in a gif. The below example illustrates converting Yahoo!'s favicon to a png using the correct transparency index of "0", which I guessed. how to detect that the ico is in fact transparent and that the transparency index is 0 ?
import urllib2
import Image
import StringIO
resp = urllib2.urlopen("http://www.yahoo.com/favicon.ico")
image = Image.open(StringIO.StringIO(resp.read()))
f = file("test.png", "w")
# I guessed that the transparent index is 0. how to
# determine it correctly ?
image.save(f, "PNG", quality=95, transparency=0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来有人认识到 PIL 并没有真正正确地读取 ICO(在将其源代码与 ICO 格式的一些研究进行协调后,我可以看到同样的事情 - 有一个 AND 位图决定透明度)
并提出了这个扩展:
http://www.djangosnippets.org/snippets/1287/
因为这对于非 django 应用程序很有用,所以我在此处重新发布了对其异常抛出的一些调整:
looks like someone recognized that PIL doesn't really read ICO correctly (I can see the same thing after reconciling its source code with some research on the ICO format - there is an AND bitmap which determines transparency)
and came up with this extension:
http://www.djangosnippets.org/snippets/1287/
since this is useful for non-django applications, I've reposted here with a few tweaks to its exception throws: