3

I have this depth image:

enter image description here

that I load with PIL like:

depth_image = Image.open('stereo.png')

If I print the mode of the image it shows mode I, that is (32-bit signed integer pixels) according to the documentation.

This is correct since the image values range from 0 to 255. I'd like to colorize this depth image for better visualization so I tried to convert it to P mode with a palette like:

depth_image = depth_image.convert('P', palette=custom_palette)
depth_image.save("colorized.png")

But the result is a black and white image like this:

enter image description here

I'm sure the palette is ok, since there are 256 colors in int format all in a single array.

I've tried to convert it to RGB before saving like:

depth_image = depth_image.convert('RGB')

Also I tried adding the palette afterwards like:

depth_image = depth_image.putpalette(custom_palette)

And if I try to save it without converting it to RGB I get a:

    depth_image.save("here.png")
AttributeError: 'NoneType' object has no attribute 'save'

So far I'll try converting the image to a numpy array and then map the colors from there, but I was wondering what was I missing out regarding PIL. I was looking around the documentation but didn't find much regarding I to P conversion.

bpinaya
  • 519
  • 6
  • 14
  • Please show your `custom_palette` – Mark Setchell Oct 21 '19 at 16:25
  • Hi, due to it being too long I coudn't add it as a comment but it's on this pastebin https://pastebin.com/JqVbF87a I take a basis from this Google's turbo color_palette located here https://gist.github.com/mikhailov-work/ee72ba4191942acecc03fe6da94fc73f and put it in a format that can be managed by PIL. I also tried something like: `.convert("P", palette=Image.ADAPTIVE, colors=256) ` And still nothing. – bpinaya Oct 22 '19 at 08:02

1 Answers1

1

I think the issue is that your values are scaled to the range 0..65535 rather than 0..255.

If you do this, you will see the values are larger than you expected:

i = Image.open('depth.png') 
n = np.array(i) 

print(n.max(),n.mean())
# prints 32257, 6437.173

So, I quickly tried:

n = (n/256).astype(np.uint8)
r = Image.fromarray(n)
r=r.convert('P') 
r.putpalette(custom_palette)     # I grabbed this from your pastebin

enter image description here

Mark Setchell
  • 146,975
  • 21
  • 182
  • 306
  • 1
    Thank you very much! I thought the issue was in the conversion, not in the image itself, weird thing is that when I opened it with Geeqie all the pixel values didn't go higher than 255. – bpinaya Oct 22 '19 at 08:27