I am processing images with PIL. When I open and convert some images to RGBA they get completely deteriorated.
One example of such an image is the following one:
$ file input_image.png
input_image.png: PNG image data, 502 x 1180, 16-bit grayscale, non-interlaced
After the conversion I get the following:
I run the following code.
from PIL import Image
img = Image.open("input_image.png")
img_rgba = img.convert("RGBA")
img_rgba.save("output_image.png")
I would expect that the output image looks like the input image.


Your original image is a 16-bit greyscale image. You can see that if you print the image:
You'll see it is in I mode, i.e. 32-bit because that's what PIL uses for 16-bit images.
You can also see that with
exiftool, if you run:When you convert it to RGBA mode, PIL makes it into RGBA8888 - i.e. 4 channels of 8 bits each and you lose data.
If you want to convert from GREY16 to RGBA8888 with solid blacks and whites, you need to rescale to range 0..255 like this: