Python 使用枕头批量创建动画GIF

Python 使用枕头批量创建动画GIF,python,image,python-imaging-library,animated-gif,pillow,Python,Image,Python Imaging Library,Animated Gif,Pillow,我正在尝试使用Python使用Pillow库批量编辑.png文件。这是我第一次使用python编写脚本,因此可能会出现许多错误和/或糟糕的编程实践 这是我目前的代码: from PIL import Image from PIL import ImageDraw from os.path import basename import os, sys path = "D:\Pokemon Game\Pokemon Eggs\Import" dirs = os.listdir( path ) bo

我正在尝试使用Python使用Pillow库批量编辑.png文件。这是我第一次使用python编写脚本,因此可能会出现许多错误和/或糟糕的编程实践

这是我目前的代码:

from PIL import Image
from PIL import ImageDraw
from os.path import basename
import os, sys

path = "D:\Pokemon Game\Pokemon Eggs\Import"
dirs = os.listdir( path )
box = (2,1,30,31)
moveup = (0, -3, -7, -11, -15, -19, -15, -9, -5, 2, 12, 14, 16, 17, 12, 8, 4, 0, -7, -13, -19, -11, -7, -5, -3)
topspace = (36, 38, 42, 46, 50, 55, 50, 44, 40, 34, 24, 22, 20, 18, 24, 28, 32, 36, 42, 48, 55, 46, 42, 40, 38)
bottomspace = (0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 10, 14, 17, 12, 8, 4, 0, 0, 0, 0, 0, 0, 0, 0)
Imagesizes = ((56, 60), (56, 58), (56, 54), (56, 50), (56, 46), (56, 41), (56, 46), (56, 52), (56, 56), (56, 60), (56, 66), (56, 64), (56, 62), (56, 60), (56, 60), (56, 60), (56, 60), (56, 60), (56, 54), (56, 48), (56, 41), (56, 50), (56, 54), (56, 56), (56, 58))

for file in dirs:
    #Pick an image file you have in the working directory
    egg = Image.open("D:\Pokemon Game\Pokemon Eggs\Import\%s" % str(file))

    #Crops white out of image
    egg = egg.crop(box)

    #Resizes image
    egg = egg.resize((56, 60), Image.NEAREST)

    #Stretch individual images
    frames = []
    for size in Imagesizes:
        frames.append(egg.resize(size, Image.NEAREST))

    #Resize canvas for each individual image - make sure it is clear
    for i,image in enumerate(frames):
        canvassize = (-20, -36 + moveup[i], 76, 60 + moveup[i])
        frames[i]=image.crop(canvassize)

    #Fix transparency
    for i,image in enumerate(frames):
        transparent_area1 = (0,0,20,96)
        transparent_area2 = (76,0,96,96)
        transparent_area3 = (0,0,96,(topspace[i]))
        transparent_area4 = (0,(96-bottomspace[i]), 96, 96)
        image.convert('RGBA')
        mask=Image.new("1", (96, 96), color=255)
        draw=ImageDraw.Draw(mask) 
        draw.rectangle(transparent_area1, fill=0)
        draw.rectangle(transparent_area2, fill=0)
        draw.rectangle(transparent_area3, fill=0)
        draw.rectangle(transparent_area4, fill=0)
        image.putalpha(mask)

    #Convert to GIF
我的目标是使无生命的鸡蛋图像最终如下图所示的动画图像:


我的代码存在的问题是,首先,第35行和第47行之间的整个部分导致透明度损失(这是由于第47行)。我不知道如何将列表(图像)转换为动画GIF。

我建议创建一个新图像,并将原始图像粘贴到其顶部,而不是使用“调整大小”作为反向裁剪。这解决了新像素的黑色问题

粘贴时,您可以提供一个遮罩,这意味着原始图像仅粘贴在遮罩指示的部分,而不是一个简单的矩形中

对于下一部分中的一些项目,将进一步解释

保存时,GIF使用附加参数“透明度”和“处置”。Disposition用于控制下一帧如何使用上一帧-新帧是否粘贴在其上?它会忽略它吗

至于创建动画GIF,Pillow具有参数“save_all”(保存多帧图像)和“append_images”(附加图像)将附加帧添加到第一幅图像

因此,将代码的下半部分更改为-

#Resize canvas for each individual image - make sure it is clear
for i,image in enumerate(frames):
    frame = Image.new('RGBA', (76 + 20, 60 + moveup[i] - (-36 + moveup[i])), '#fff')
    image = image.convert('RGBA')
    frame.paste(image, (20, 36 - moveup[i]), image)
    frames[i] = frame

#Convert to GIF
frames[0].save('out.gif', save_all=True, append_images=frames[1:], transparency=0, disposal=2)

GIF透明度和普通alpha透明度是完全不同的东西,我不确定枕头是否能很好地处理转换-我从未测试过它。根据我以前使用PIL的经验,我猜它完全搞砸了。@Markransem我的代码没有达到创建GIF的阶段,我很难增加画布的大小,因为我会调整画布的大小以使其更大,如果新像素变为黑色,为了解决这个问题,我尝试在蒙版对象中绘制透明矩形,然后对原始图像使用蒙版,问题是现在任何原始透明像素都变为白色,只有来自透明矩形的像素保持透明。