Skip to main content
 首页 » 编程设计

Python Pillow .ico 转换为 .gif

2024年11月24日7lovecherry

我使用了 python pillow 库,因为我需要将 .ico 转换为 .gif

所以我使用 Pillow 6.1.0 ver 和 Python 3.7.0 我执行以下操作:

from PIL import Image 
 
im = Image.open("a.ico") 
im.show() 
 
im.save("a.gif") 

.ico文件有透明背景

但是im.show()加载的图片是黑色背景 所以最后我得到了黑色背景的 .gif 文件

我想知道如何获得透明的.gif 转换文件!

请您参考如下方法:

im.show() loaded Image is black background

这是因为 Image.show() 在显示之前将图像转换为 .bmp (位图)。 PIL 中的Bitmap 不支持 Alpha(可变透明度/不透明度)。因此,当您尝试使用 Image.show() 显示图像时,.ico 图像中的 alpha 数据会丢失。

解决方案:- 不要使用 Image.show() 查看包含 alpha 值的图像,而是保存图像然后查看它(通过一些图像处理包,如 photoshop 等,或使用操作系统的内置图像查看器,如 Windows 10 中的 Photos)。

I want to know how i get transparent .gif conversion files!

.gif(图形交换格式)不支持 Alpha channel 。因此,您无法在保留透明区域的同时将 .ico 文件转换为 .gif。因此,不建议将具有 Alpha channel 的图像保存为 .gif,因为它最终会产生伪像并会降低原始图像的质量。

解决方案:- 我建议您选择支持 alpha channel 的格式,如 .png 或 .jp2。它们也支持大范围的颜色(与 GIF 不同,后者仅支持 256 种离散颜色(8 位调色板))。