6

I have some images saved to PNG and unfortunately because of the encoding they always aren't 3 channel (this seems to be part of LodePNG).

When using PIL I will load images in and most will be (256,256,3) but ones where there isn't much color information end up being (256,256) (as far as I can tell this is a LodePNG thing). Since the majority of my images have 3 channels I would prefer to do that. But doing img.convert('RGB') does not accomplish the task.

Is there a way I can force PIL to open a png image to have 3 channels?

Note: I can open the images with preview and see that they are missing the third channel by looking in inspector. These files are also saved out with lodePNG.

Steven Walton
  • 505
  • 6
  • 20

1 Answers1

17

Yes.

from PIL import Image
im = Image.open("image.png").convert('RGB')

I've subsequently made a better explanation here.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I have to say I'm a little shocked that this works. Why is this different then `im = Image.open("image.png"); im = im.convert('RGB')`? Because that's what I was doing before and switching to a single line I have absolutely no issue anymore. – Steven Walton Aug 20 '19 at 17:09