【问题标题】:How can I modify this perfect code for my own code?我如何为自己的代码修改这个完美的代码?
【发布时间】:2022-12-10 22:34:33
【问题描述】:

我有这个完美的 Python 枕头代码编辑 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 的每一帧并保存它。

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

谢谢!

【问题讨论】:

  • 你已经拥有了所有的构建块。搏一搏。如果您遇到困难,请随时回来具体的关于您正在使用的代码的问题。
  • 谢谢 Chris 的激励话语,我真的不了解第一个代码及其作用,我需要的只是一个指向正确方向的指针,我自己也许可以做到
  • 你不需要了解每一件事。你能弄清楚第一个 sn-p 如何遍历 GIF 中的帧吗?你会如何改变它对每一帧的作用?
  • 好的。首先感谢您花时间回复。我在第一个代码中看到 original 指的是整个动画 GIF,所以我不需要在那里做任何事情。现在我看到的问题是这段代码可能打算创建一个彩色框架并将其应用于等效的原始框架。这是我丢失它的地方,变量“new_frame”似乎指的是颜色编辑,我似乎无法找到对原始 gif 实际帧的引用......
  • 解决方案应该类似于“删除这些行,并在此处放入函数“Image_Watermark”。因此它可以循环并编辑每一帧”

标签: python image python-imaging-library gif


【解决方案1】:

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

编辑 Image_Watermark 函数以从 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) 函数。

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

【讨论】:

    猜你喜欢
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多