实际上,我正在使用python做一些实验,但是我到了要在透明GIF上添加图像尺寸的位置。
我遇到bad transparency mask
错误。
代码-
from PIL import Image, ImageSequence
background = Image.open(...)
animated_gif = Image.open(...)
frames = []
for frame in ImageSequence.Iterator(animated_gif):
frame = frame.copy()
frame.paste(background, mask=bg)
frames.append(frame)
frames[0].save('output.gif', save_all=True, append_images=frames[1:])
答案 0 :(得分:0)
import Image
background = Image.open("test1.png")
foreground = Image.open("test2.png")
background.paste(foreground, (0, 0), foreground)
background.show()
我将解释.paste()函数的参数。 首先-要粘贴的图像 第二个坐标 第三-这表示将用于粘贴图像的遮罩。如果您传递具有透明度的图像,则alpha通道将用作遮罩。
如果这不是您想要执行的操作,请根据需要添加评论。
答案 1 :(得分:0)
这是我问题的答案...
from PIL import Image, ImageSequence
background = Image.open("img.jpg")
animated_gif = Image.open("GIFF.gif")
frames = []
for frame in ImageSequence.Iterator(animated_gif):
output = background.copy()
frame_px = frame.load()
output_px = output.load()
transparent_foreground = frame.convert('RGBA')
transparent_foreground_px = transparent_foreground.load()
for x in range(frame.width):
for y in range(frame.height):
if frame_px[x, y] in (frame.info["background"], frame.info["transparency"]):
continue
output_px[x, y] = transparent_foreground_px[x, y]
frames.append(output)
frames[0].save('output.gif', save_all=True, append_images=frames[1:-1])