17

This is my issue:

import Image
im = Image.open("1.png")
im.show()
print im.mode
im.convert("RGBA").save("2.png")

Well, with my image you can see the difference.
My question is: how do I convert it properly?

Image: original

Result: result

NOTE: The original image has a semi-transparent glow, the result has a solid green "glow"

mDroidd
  • 1,213
  • 4
  • 17
  • 25
  • Could you explain what the problem is, perhaps? – mwcz Sep 17 '12 at 15:37
  • you don't actually say what the difference is. Is the image you include a before or after? – Andrew Cox Sep 17 '12 at 15:38
  • Tested it. Did not see any difference. What python and pil version do you use ? – Nicolas Barbey Sep 17 '12 at 15:51
  • There is a difference if you open the file outside python, but not between the two img.show() – MatthieuW Sep 17 '12 at 16:13
  • What program(s) are you using to display the images? Can you describe with more detail what is wrong, or provide a screenshot that shows the before and after images? – Silas Ray Sep 17 '12 at 16:33
  • Sorry, updated. the problem is the semi-transparency, wich becomes solid. See the added picture – mDroidd Sep 17 '12 at 16:38
  • 1
    See [this](http://stackoverflow.com/questions/1233772/pil-does-not-save-transparency) Q&A. Read both answers and their comments! – Junuxx Sep 17 '12 at 16:43
  • That was ofcourse the first thing I tried before posting: "im.convert("RGBA").save("BAR2.png", **inf) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1439, in save save_handler(self, fp, filename) File "/usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.py", line 537, in _save raise IOError("cannot use transparency for this mode") IOError: cannot use transparency for this mode" – mDroidd Sep 17 '12 at 16:52
  • 1
    @mDroidd: FWIW the resultant RGBA actually _does_ have an alpha layer that is set to transparency set on exactly the same pixels as those in the palleted image. In other words the "glow" pixels in the palleted image are not marked transparent at all -- which I assume is why they aren't in the RGBA result. I also noted that I see the glow only when I view the image in a browser, but not in the image editor I usually use (Photoshop). This doesn't make sense and I'm trying to figure-out what going on and why. – martineau Sep 18 '12 at 19:21
  • Well, for now it seems a suitable option for me to open the image with gdk and save it - that will convert it to RGB without losing partial transparency. – mDroidd Oct 14 '12 at 07:40

4 Answers4

9

This issue was reported here:

https://bitbucket.org/effbot/pil-2009-raclette/issue/8/corrupting-images-in-palette-mode

In March 2012, a comment says it's now fixed in development version of PIL. The most recent released version is 1.1.7, so the fix won't be available until 1.2 comes out. PIL updates very slowly, so don't expect this to come out soon.

jterrace
  • 64,866
  • 22
  • 157
  • 202
5

Unfortunately your PNG image is a type that PIL doesn't handle very well - a paletted image with an alpha channel. When you open the image, the alpha is thrown away and there's no way to get it back.

This is different from the usual palette transparency where one index of the palette is used to denote fully transparent pixels.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Relevant places to look: [PngImagePlugin tRNS loader](https://github.com/python-imaging/Pillow/blob/master/PIL/PngImagePlugin.py#L527-529) where it only reads 1 byte instead of an alpha block, and [PNG tRNS spec](http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.tRNS) showing that it should be reading a block, not a single value – jterrace Sep 17 '12 at 18:10
2

You could use scipy.misc.imread:

img = scipy.misc.imread(filename, mode='RGBA')
img = Image.fromarray(img)
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • I think this was recently deprecated from scipy without replacement. I'm currently on mobile in vacation, so it's hard to check for me. – Martin Thoma Dec 07 '18 at 14:30
0

Your problem is that you do not provide info about what PIL should use as source of ALPHA channel.

PIL will not on its own add transparency to your image.

What part of your image you want to be transparent?

przemo_li
  • 3,932
  • 4
  • 35
  • 60
  • The part that's transparent and/or semi-transparent in the original "P" image :P And auto-detection too, as this is not the only image I need to convert... – mDroidd Sep 17 '12 at 16:51
  • Sorry but for me both images look identical. Can you mark on the 3rd image parts that should be transparent with red? – przemo_li Sep 17 '12 at 20:08