Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

Generating png with transparent background from eps logo

Open egpotter opened this issue 10 years ago • 4 comments

What did you do?

Tryied to generate a .png with transparent background from an .eps logo.

What did you expect to happen?

I expected the background to be transparent. I was able to do that with imagemagick easily, but I couldn't figure out a way to do it with Pillow.

What actually happened?

The background is always white.

What versions of Pillow and Python are you using?

Python 3.4.5 and Pillow 3.3.3 Please include code that reproduces the issue and whenever possible, an image that demonstrates the issue. The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as plone, django, or buildout, try to replicate the issue just using Pillow.

from PIL import Image


file_name = '/home/eduardo/Downloads/Volkswagen_logo.eps'
out_file = 'test.png'

image = Image.open(file_name)
image.save(out_file)

I also tried using mask for adding an alpha channel, but could not achieve this.

egpotter avatar Oct 13 '16 14:10 egpotter

You should be able to add an alpha mask with Image.putalpha. Note that the alpha mask needs to be a mode 1 or L image of the same size as the original image. After you do that, the image mode should be RGBA, at which point you'll have a transparent image when you save in png format.

The code that you have posted is likely getting an RGB image from the eps file, then saving that back out in PNG format. (but it's hard to tell without actually seeing the image)

wiredfool avatar Oct 14 '16 15:10 wiredfool

I have already tried adding the alpha via putalpha. The code should work with any eps image without background.

from PIL import Image


file_name = '/home/eduardo/Downloads/Volkswagen_logo.eps'
out_file = 'test.png'

image = Image.open(file_name)
imgSize = image.size
mask=Image.new('L', image.size, color=255)
image.putalpha(mask)
image.save(out_file)

Either I get a white background, or a transparency that is also applied to the image (logo) itself.

This is the logo I'm tring to open and convert to png via pillow. Maybe I'm being really stupid at some point, but I already spent some time into it and could not solve this.

egpotter avatar Oct 14 '16 18:10 egpotter

The mask that you're adding is fully opaque, so it's not actually affecting anything. Try adding some pixels other than 255, and those will be transparent (fully: 0, partially: anything else)

wiredfool avatar Oct 15 '16 12:10 wiredfool

Hmm ok! But this value would change depending on the image... It would solve my problem now but could end up making some parts of pictures transparent when i did not want that. Thanks very much for your help!

egpotter avatar Oct 17 '16 11:10 egpotter