Transparency not working consistently while saving

2019-07-24 17:47发布

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.

Here is the script

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()

enter image description here image1.png

enter image description here image2.png

And the font is here https://bipuljain.com/static/images/Ultra-Regular.ttf

And the output with issue. enter image description here

And output working fine. enter image description here

1条回答
何必那么认真
2楼-- · 2019-07-24 18:10

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

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

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.

查看更多
登录 后发表回答