8

I have a numpy array that is written to an image, a RGB colormap added as a palette, and all that remains is a transparency channel (256 values) on top. I have tried converting to RGBA, LA, and other ways around it but, I cannot figure out how to add this multi-value channel on top as a palette.

Here is an example that I have that adds a single-value channel of transparency:

# data = numpy array 1624x3856
im = Image.fromarray(data)
im = im.convert('P')
# cmap is a 768-valued RGB array
im.putpalette(my_cmap)
im.save('filename.png', transparency=0)

The channel I want to save is as follows:

# len(alpha) = 256
alpha = [0,255,255,255...255,255,255]

Any help would be greatly appreciated.

monkcoder
  • 83
  • 1
  • 5

2 Answers2

6

Here is a simple example on how to ensure a Pillow image is RGBA :

img = Image.open("SOME_RGB_IMAGE.png")

if img.mode == "RGB":
    a_channel = Image.new('L', img.size, 255)   # 'L' 8-bit pixels, black and white
    img.putalpha(a_channel)
snoob dogg
  • 2,491
  • 3
  • 31
  • 54
4

Prepare a correct RGBA format for working with an Alpha-channel

After preparing the RGB-part ( as an issue seen from the data.shape above ) your raster-image will have to get the corresponding Alpha-layer.

To do that, add a call to an instance method

Image.putalpha( anAlphaLAYER )

That adds / replaces the alpha-layer in your image. If the image does not have an alpha layer, it’s converted to LA or RGBA, so have the RGB-part ready before call to this method. The new layer ( anAlphaLAYER ) must be either L or 1.

So be sure to have anAlphaLAYER.shape matching your im.size ( X, Y ). Another possibility is to create alpha-layer from an integer value and modify it's cell-values ad-hoc.

Having done the full RGBA-format as early as in the numpy.array, the whole issue would not make you any troubles.

user3666197
  • 1
  • 6
  • 50
  • 92
  • 1
    So, do you mean `Image.new('L', im.size, 255)`? I've tested this with the size and it is the same as the original image. – monkcoder Oct 24 '14 at 15:41
  • What do you mean by _Having done the full RGBA-format as early as in the numpy.array_? – monkcoder Oct 24 '14 at 16:10
  • ***RGBA-format as early as in the numpy*** reflects the fastest ever practice / approach, i.e. to manipulate [R,G,B]+[A]-layers straight in numpy array ( 1624, 3856, 4 ), as opposed to the slower PIL/Pillow pixel-by-pixel mode, and only finally convert/save into the requested fileformat. All computer vision tools & utilities work this way. – user3666197 Oct 24 '14 at 16:18
  • Ah. And I would create an image using the fromarray method and then convert to 'P' type and save? I wouldn't have to use the putpallete method nor the transparency=0 argument and still retain a paletted png with all channels still in tact? – monkcoder Oct 24 '14 at 16:20
  • Or you may use OpenCV2 tools to go faster for the same. – user3666197 Oct 24 '14 at 16:23
  • I will try that second method and let you know. Currently getting the putalpha method reviewed as well although I had to modify as the alpha layer was not a single valued image (i.e., I had to make 0's where I wanted them). – monkcoder Oct 24 '14 at 16:30