【发布时间】:2021-12-19 20:24:46
【问题描述】:
好的,所以我有一些代码可以接受用户输入,获取每个字母对应的 gif(一个跳舞的字母)并将它们拼接在一起形成一个单词。基本上有点像动画字体。
我的问题是,使用 PIL,它偶尔会用红色填充其中一个字母的背景大约 1 帧,如下所示:
这是我生成的代码
import sys
from PIL import Image
from PIL import GifImagePlugin
from io import BytesIO
def text(str, procid):
t = []
for c in str:
t.append('letters/' + c + '.gif')
images = [Image.open(x) for x in t]
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
x_offset = 0
imgs = []
for frame in range(0, images[0].n_frames):
new = Image.new('RGBA', (total_width, max_height))
x_offset = 0
for im in images:
im.seek(frame)
new.paste(im, (x_offset,0))
x_offset += im.size[0]
imgs.append(new)
img_io = BytesIO()
new.save(img_io, 'GIF', transparency=0, save_all=True, append_images=imgs, loop=0, disposal=2,duration=1,optimize=True)
img_io.seek(0)
return img_io
关于它为什么这样做的任何想法,是否有可能解决这个问题?
编辑:我附上了用于创建动画“TEST”的输入 GIFS
e.gif https://i.stack.imgur.com/PQfMK.gif
s.gif https://i.stack.imgur.com/7PQp5.gif
t.gif https://i.stack.imgur.com/DMLrg.gif
编辑 2:所有字母的帧数都相同,红色背景在 S.gif 中的任何地方都没有,这意味着它必须是 PIL 问题或我的代码有问题。
【问题讨论】:
-
如果有帮助,我检查了每个 GIF 并且帧不包含此工件。此外,我合并为一个的每个 GIF 的帧数因字母而异(可能是原因)。
-
如果没有找到框架,
im.seek(frame)会返回什么? -
也许它与字母 S 的 .gif 有关 - 尝试将其更改为其他没有问题的文件之一。
-
当您不共享输入图像并且您的代码不完整且没有
import语句时,我们如何为您提供帮助? -
你能发布单独的 gif 吗?
标签: python python-imaging-library gif