怎么把PNG图像转换成透明的GIF? [英] How to convert PNG images to a transparent GIF?

查看:246
本文介绍了怎么把PNG图像转换成透明的GIF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将具有透明背景的PNG图像列表转换为GIF,同时保持背景透明性.我采用了找到的这段代码,并对其进行了修改:

  import os从PIL导入图像#创建框架帧数= []路径=目录/到/我/png/图像"对于os.listdir(path)中的帧:new_frame = Image.open(路径+"/" +帧)frame.append(new_frame)#保存到GIF文件中frame [0] .save(path +``/../output/animation.gif",format ='GIF',append_images = frames [1:],save_all =是,持续时间= 41,循环= 1,透明度= 0) 

它将打开文件夹中的所有PNG图像,并将其导出到GIF,但是背景为黑色.我看过

您的最小化代码如下所示:

从PIL导入图像

 框架= [Image.open('red.png'),Image.open('green.png'),Image.open('blue.png')]帧[0] .save('test.gif',format ='GIF',append_images = frames [1:],save_all =是,持续时间= 200,循环= 0,透明度= 0) 

生成的GIF实际上并不反映单个PNG的透明度,GIF完全损坏:

将转换添加到模式 PA 时,代码可能如下所示:

从PIL导入图像

 框架= [Image.open('red.png'),Image.open('green.png'),Image.open('blue.png')]帧= [frame.convert('PA')表示帧中的帧数]帧[0] .save('test.gif',format ='GIF',append_images = frames [1:],save_all =是,持续时间= 200,循环= 0,透明度= 0) 

而且,结果很好,保持了透明性:

我不知道,该路线是否适用于任意PNG,但是值得对您的图像进行测试,不是吗?如果这不起作用,则需要提供一些输入图像以进行进一步测试.

最后一种方法可能是将PNG中的所有透明像素替换为某种颜色,例如纯黄色.以后保存GIF时,需要确保所有图像的调色板在相同的索引处存储该纯黄色,然后最后将 transparency 设置为该索引.

  ----------------------------------------系统信息----------------------------------------平台:Windows-10-10.0.16299-SP0的Python:3.9.1枕头:8.1.0---------------------------------------- 

I'm trying to convert a list of PNG images with a transparent background to a GIF, while keeping the background transparency. I took this bit of code I found, and adapted it:

import os
from PIL import Image

# Create the frames
frames = []

path = "directory/to/my/png/images"
for frame in os.listdir(path):
    new_frame = Image.open(path + "/" + frame)
    frames.append(new_frame)

# Save into a GIF file
frames[0].save(path + "/../output/animation.gif", format='GIF',
               append_images=frames[1:],
               save_all=True,
               duration=41, loop=1, transparency=0)

It is opening all the PNG images I have in a folder, and export them to a GIF, but the background is black. I have looked at the PIL documentation, but I don't seem to understand how the transparency parameter works, or I think I am using it wrong.

解决方案

First of all, the GIF format does not support alpha-channel transparency like PNG does. You can only select one out of the 256 possible colors in a GIF to be transparent. So, you also don't get any smooth transparencies, pixels are either fully transparent or opaque.

When dealing with Image objects having mode RGBA, try to convert all your images to mode PA before saving. Maybe, that helps automagically.

Let's assume we have the following three images:

Your minimized code would look like this:

from PIL import Image

frames = [Image.open('red.png'), Image.open('green.png'), Image.open('blue.png')]

frames[0].save('test.gif', format='GIF',
               append_images=frames[1:],
               save_all=True,
               duration=200, loop=0, transparency=0)

The resulting GIF in fact doesn't reflect the transparency from the single PNGs, the GIF is totally corrupted:

Adding the conversion to mode PA, the code might look like this:

from PIL import Image

frames = [Image.open('red.png'), Image.open('green.png'), Image.open('blue.png')]

frames = [frame.convert('PA') for frame in frames]

frames[0].save('test.gif', format='GIF',
               append_images=frames[1:],
               save_all=True,
               duration=200, loop=0, transparency=0)

And, the result is fine, transparency is maintained:

I don't know, if that route works for arbitrary PNGs, but it's worth testing with your images, isn't it? If that doesn't work, you need to provide some of your input images for further testing.

The final approach could be to replace all transparent pixels in your PNGs with a certain color, let's say solid yellow. When saving the GIF later, you'd need to make sure, that all images' palettes store that solid yellow at the same index, and then finally set transparency to that index.

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.9.1
Pillow:        8.1.0
----------------------------------------

这篇关于怎么把PNG图像转换成透明的GIF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆