Hello Sebi and Peter –
I have an PNG in Fiji that has all 0-1 pixel values. But every time I save it and reopen it as PNG the values are now 0-255
At issue is the fact that the PNG format does not store the image’s “Display range” metadata, and therefore ImageJ has to make a choice when storing an image with a display range as PNG.
As you see, ImageJ chooses to store an image with rescaled pixel values so that when you reopen it, you get the same displayed appearance, but lose the original pixel values.
If your use case permits you to use TIFF format, that would be the cleanest way to go. TIFF stores the display range, so when you reopen it you get both the original appearance and the original pixel values.
- Image → Type → RGB Color
- Image → Type → 8-bit Color
Note that the “8-bit Color” conversion does work for pixel values of 0 and 1, but more or less by happenstance. In general, converting color images that are in fact grayscale to 8-bit Color is quite lossy. (If your use case is restricted to 0-1 grayscale images you should be fine.)
Here is an IJ Macro that illustrates saving as TIFF and also the artifacts introduced by using 8-bit Color. It starts with creating a “half-ramp” 8-bit image whose pixel values range up to 127 instead of 255. (Actually up to 128, but never mind that.)
newImage (“hramp”, “8-bit ramp”, 256, 256, 1); run (“Divide…”, “value=2”); saveAs (“png”, “hramp_png_raw.png”); setMinAndMax (0, 127); saveAs (“png”, “hramp_png.png”); saveAs (“tiff”, “hramp_tif.tif”); run (“RGB Color”); run (“8-bit Color”, “number=256”); saveAs (“png”, “hramp_8c.png”);
Here is the original half-ramp image before setting the Display range, save as PNG. So far, so good.
hramp_png_raw.png:
Here is the PNG version after setting the Display range:
hramp_png.png:
It displays “correctly” but the pixel values have changed.
Here is the TIFF version. You will probably have to download the TIFF version and open it in ImageJ to see it:
hramp_tif.tif:
hramp_tif.tif (64.2 KB)
When opened in ImageJ it should display correctly and have the correct pixel values.
Lastly, here is the 8-bit Color version:
hramp_8c.png:
It displays with the correct display range, but you can see the grayscale quantization artifacts due to the lossy 8-bit-color conversion algorithm. (Also, pre-LUT, the pixel values are totally garbled, and post-LUT, they range up to 255 instead of 127.) Again, the 8-bit-color approach really only works by happenstance for the 0-1 pixel-value case.
Thanks, mm
