Skip to content
Advertisement

Does Image.open() distort grayscale PNG images?

Recently, I’ve been trying to load some grayscale images into my Python program using Pillow’s Image.open(). However, I’ve been finding that the image gets distorted, and all the values polarized.

This is the image generated by the Mac preview:

enter image description here

And, this is the image generated by the Image.open() function:

enter image description here

My code is as follows:

JavaScript

And, the characteristics of the image via preview is:

enter image description here

I believe that this may have something to do with either the color space of the image or the lack of the alpha channel, as an image that had both was loaded in correctly.

Advertisement

Answer

No float values, but your CT scan is a 16-bit unsigned integer grayscale image. When opening this image using Image.open, the corresponding mode is given with I, which is wrong here, since I is “32-bit signed integer pixels”, whereas we’d rather like to have mode I;16, which is “16-bit unsigned integer pixels”.

JavaScript

Output:

JavaScript

Now, in general, plt.imshow is able to show Pillow Image objects, but there seems to be an issue when showing I mode Image objects: It seems (see below image, see the lower, right corner of the plot window), that the assumed 32 bits are interpreted as RGBA values. So, even setting vmin and vmax in plt.imshow doesn’t work here.

Strangely, when converting to mode I;16, the whole image is (still) clipped at 32767 – which I don’t understand and can’t explain. So, the corresponding plot is still faulty (see below image).

The only way to get a proper output from plt.imshow seems to be to convert the image to some NumPy array, explicitly setting the dtype to np.uint16, and then convert back to a Pillow Image object with mode I;16:

JavaScript

Output:

Output

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement