在PIL中保存GIF时,透明度无法始终如一地工作 [英] Transparency not working consistently while saving GIF's in PIL

查看:126
本文介绍了在PIL中保存GIF时,透明度无法始终如一地工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写覆盖图像并使背景透明的脚本.输出应该是GIF格式.

I am working on script that writes over images and makes the background transparent. Output is supposed to be in GIF format.

该脚本有效,但对于某些图像,透明度无法正常工作.

The script works but for certain images the transparency is not working as expected.

这是脚本

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

CANVAS_HEIGHT = 354
CANVAS_WIDTH = 344


def get_text_mask():
    font_style_path = 'Ultra-Regular.ttf'

    text_mask_base = Image.new('L', (CANVAS_WIDTH, CANVAS_HEIGHT), 255)
    text_mask = text_mask_base.copy()
    text_mask_draw = ImageDraw.Draw(text_mask)
    font = ImageFont.truetype(font_style_path, 94)
    text_mask_width, text_mask_height = text_mask_draw.multiline_textsize("1000\nUsers",
                                                                          font=font)

    text_mask_draw.multiline_text(((CANVAS_WIDTH - text_mask_width) / 2,
                                   (CANVAS_HEIGHT - text_mask_height) / 2),
                                  "1000\nUsers",
                                  font=font,
                                  fill=0,
                                  align='center')

    return text_mask


def run():
    images = ['image1.png', 'image2.png']
    for index, original_image in enumerate(images):
        image = Image.open(original_image)
        blank_canvas = Image.new('RGBA', (CANVAS_WIDTH, CANVAS_HEIGHT), (255, 255, 255, 0))
        text_mask = get_text_mask()
        final_canvas = blank_canvas.copy()
        for i in xrange(0, CANVAS_WIDTH, image.width):
            for j in xrange(0, CANVAS_HEIGHT, image.height):
                final_canvas.paste(image, (i, j))

        final_canvas.paste(text_mask, mask=text_mask)
        final_canvas.convert('P', palette=Image.ADAPTIVE)
        final_canvas.save("output-{}.gif".format(index), format="GIF", transparency=0)

run()

image1.png

image1.png

image2.png

image2.png

字体在这里 https://bipuljain.com/static/images/Ultra-Regular.ttf

和有问题的输出.

并且输出正常.

推荐答案

问题是您的原始图像"包含的索引颜色与GIF用来表示此像素透明"的索引颜色相同.

The problem is that your "original image" contains the same index-color that the GIFs use to signify "this pixel is transparent".

Gif是基于调色板的"-此调色板中的一个索引被指定为此透明"(请参阅​​fe

Gifs are "palette-based" - one index into this palette is designated as "this is transparent" (see f.e. https://en.wikipedia.org/wiki/GIF)

因此,如果您将pure blackpure white指定为透明的颜色索引,并且您的源图像已经包含具有这种确切颜色的像素,则它们也将变得透明.

So if you specify pure black or pure white as color-index that is transparent and your sourceimage already contains pixels with this exact color, they get to be transperent as well.

为避免这种情况,您可以对源背景图像进行采样,然后选择不存在"的颜色作为透明度颜色-这样就永远不会出现在生成的图像中.

To avoid this, you could sample your source background-image and choose a "non-existent" color as transparency-color - this would never get to be in your resulting image.

您还可以更改源图像的像素值-检查所有像素,并将所有背景"更改很小的一部分,以免它们变得不透明.

You could also change your source images pixel values - check all pixel and change all "background-ones" a tiny fraction off so they do not get translucent.

这篇关于在PIL中保存GIF时,透明度无法始终如一地工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆