繁体   English   中英

我如何为自己的代码修改这个完美的代码?

[英]How can I modify this perfect code for my own code?

我有这个完美的 Python 枕头代码,它用一些 colors编辑 GIF 文件的每一帧并保存它

def process_image(filename, color_depth):
    original = Image.open(filename)

    new = []
    for frame_num in range(original.n_frames):
        original.seek(frame_num)
        new_frame = Image.new('RGBA', original.size)
        new_frame.paste(original)
        new_frame = new_frame.convert(mode='P', palette=Image.ADAPTIVE, colors=color_depth)
        new.append(new_frame)

 

    new[0].save('Images/new.gif', append_images=new[1:], save_all=True, loop=0)

我有这段代码可以编辑单个图像并在其上添加水印


def Image_Watermark(directory, filename):
    #Opening Image & Creating New Text Layer
    img = Image.open(directory + "/" + filename).convert("RGBA")
    txt = Image.new('RGBA', img.size, (255,255,255,0))

    #Creating Text
    text = "WATERMARK EXAMPLE"
    font = ImageFont.truetype("arial.ttf", 27)

    #Creating Draw Object
    draw = ImageDraw.Draw(txt)

    #Positioning of Text
    width, height = img.size
    # textwidth, textheight = d.textsize(text, font)
    # x=width/2-textwidth/2
    # y=height-textheight-300

    # Loop for Multiple Watermarks
    y=200
    for i in range(7):
        x=random.randint(0, width-300)
        y+=random.randrange(0,int(height/8), 19)+random.randint(0,100)
        draw.text((x,y), text, fill=(255,255,255, 75), font=font)

    #Combining both layers and saving new image
    watermarked = Image.alpha_composite(img, txt)
    watermarked.save(filename)

现在第一个代码完美地编辑了 gif,我不想改变颜色,相反我想要第二个代码所做的,添加水印,到 gif 的每一帧并保存它。

这似乎是我需要做的两种代码的简单组合,但我无法弄清楚如何,有人可以帮助我完成这项工作吗?

谢谢!

将两个函数放在同一个文件中(使用上面声明的Image_Watermark() function process_image()

编辑 Image_Watermark function 以从 process_image 获取图像而不是从文件中读取:

def Image_Watermark(img):
    # use img passed as argument instead of reading from a file
    txt = Image.new('RGBA', img.size, (255,255,255,0))

    #Creating Text
    text = "WATERMARK EXAMPLE"
    font = ImageFont.truetype("arial.ttf", 27)

    #Creating Draw Object
    draw = ImageDraw.Draw(txt)

    #Positioning of Text
    width, height = img.size
    # textwidth, textheight = d.textsize(text, font)
    # x=width/2-textwidth/2
    # y=height-textheight-300

    # Loop for Multiple Watermarks
    y=200
    for i in range(7):
        x=random.randint(0, width-300)
        y+=random.randrange(0,int(height/8), 19)+random.randint(0,100)
        draw.text((x,y), text, fill=(255,255,255, 75), font=font)

    #Combining both layers and return the new image
    watermarked = Image.alpha_composite(img, txt)
    return watermarked

将 process_image 修改为以下内容:

def process_image(filename):
    original = Image.open(filename)

    new = []
    for frame_num in range(original.n_frames):
        original.seek(frame_num)
        new_frame = Image.new('RGBA', original.size)
        new_frame.paste(original)

        # call the watermark function instead of changing colors
        new_frame = Image_Watermark(new_frame)
        new.append(new_frame)



    new[0].save('Images/new.gif', append_images=new[1:], save_all=True, loop=0)

使用文件路径调用process_image(image_file_path) function。

注意:我还没有测试它是否有效。 如果您遇到任何错误,请在评论中发表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM