I want to open color image with PIL.Image.open() and convert it to a numpy array for further analysis.
The image I'm using can be downloaded here.
I've managed to open the image and display it full colour in Jupyter Notebook with no issue, but as soon as I use the getpixel method, or convert it to an array with numpy, things go wrong.
from PIL import Image
t = Image.open('radar.png')
display(t)
This works fine so far, with the transparent part of the image showing as white on the screen.
t.show()
First unexpected behaviour, the transparent part has been replaced with all black when it opens in the file viewer. The colours are still seen correctly.
t.getpixel((200, 200))
248
Second unexpected behaviour, I was expecting to see a tuple but instead it's a single number.
import numpy as np
im2arr = np.asarray(t)
im2arr.shape
(512, 512)
Third unexpected behaviour, was expecting to get a three dimensional array.
arr2im = Image.fromarray(im2arr)
display(arr2im)
Fourth unexpected behaviour, the image is now displayed as black or white (not grayscale). It is black where it should be transparent, and white where there should be colours.
My suspicion is that PIL.Image is being somehow tricked when it opens the image the first place, as the first pixel is transparent or missing? It doesn't register as an RGBA format. I have tried the same code with full colour png that I created in paint, and it all displays correctly including 3 dimensional numpy array.