bue
bue

Reputation: 633

python PIL png text get messed up

I would like to merge multiple png that I generated with matplotlib into a new png. Now, when I load and save any png with PIL the text gets totally screwed up and becomes unreadable.

from PIL import Image

img_zraw_box = Image.open('tmp_zraw_box0.png')
img_result = Image.new('RGB', (img_zraw_box.width, img_zraw_box.height))
img_result.paste(img_zraw_box, (0, 0))
img_result.save('tmp_zraw_box1.png')

original matplotlib png: enter image description here

png open, past and saved with PIL: enter image description here

Has anyone and idea what goes wrong, and how I could fix this? Thank you, Elmar

Upvotes: 1

Views: 171

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 208077

I think you need to add the mask parameter to your PIL paste() call so that the alpha channel is properly respected:

from PIL import Image
img_zraw_box = Image.open('tmp_zraw_box0.png')

# Create a solid white background to paste onto
img_result = Image.new('RGB', (img_zraw_box.width, img_zraw_box.height), color='white')

# Paste with alpha mask
img_result.paste(img_zraw_box, (0, 0), mask=img_zraw_box)
img_result.save('tmp_zraw_box1.png')

Upvotes: 1

Related Questions