Posted by
Wayne Rasband on
URL: http://imagej.273.s1.nabble.com/16-bit-gray-PNG-write-broken-tp3695138p3695141.html
Here is a plugin that saves a 16-bit image as a 16-bit PNG.
-wayne
import ij.plugin.*;
import ij.*;
import ij.io.*;
import ij.process.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
/** Saves a 16-bit image in PNG format. */
public class PNG16_Writer implements PlugIn {
public void run(String path) {
ImagePlus imp = IJ.getImage();
if (imp.getBitDepth()!=16)
{IJ.error("16-bit image reqired"); return;}
SaveDialog sd = new SaveDialog("Save as PNG...",
imp.getTitle(), ".png");
String name = sd.getFileName();
if (name==null) return;
String dir = sd.getDirectory();
try {
ShortProcessor sp = (ShortProcessor )imp.getProcessor();
BufferedImage bi = sp.get16BitBufferedImage();
ImageIO.write(bi, "png", new File( dir+name));
} catch (Exception e) {
IJ.showMessage("PNG16 Writer", "An error occured writing
the file.\n \n" + e);
}
}
}
On Sep 9, 2008, at 3:22 PM, G. Jehle wrote:
> Quoting Wayne Rasband <
[hidden email]>:
>
>> On Sep 9, 2008, at 1:48 PM, G. Jehle wrote:
>>
>>> Hi everyone,
>>>
>>> I'm running ImageJ 1.39u on Ubuntu GNU/Linux (x86_64).
>>>
>>> Whenever I try to save a simple '16-bit grayscale' image
>>> as PNG, ImageJ saves a '8-bit/color RGB' image instead.
>>>
>>> I generated a 16-bit gray PNG using Matlab.
>>> Opening the PNG in ImageJ; works just fine with ImageJ
>>> recognizing the file as 16-bit gray, all values look ok.
>>>
>>> However, using 'File > Save As > PNG...' on the very
>>> file just opened results in a '8-bit/color RGB' PNG to be
>>> saved.
>>>
>>> Any ideas to why this might happen?
>>
>> ImageJ 1.41d and later saves 16-bit and 32-bit images as 8-bit PNGs.
>> Earlier versions saved as RGB. ImageJ converts to 8-bits when saving
>> in
>> PNG format so that the images are more likely to display correctly in
>> other applications. Save in TIFF format if you need to preserve 16-bit
>> or 32-bit values.
>
> Thanks for the info wayne.
>
> Although I have to say that, IMHO, this is bad.
> Changing bit depth without giving a warning, even more so
> in a case where the image format actually supports the higher
> bit depth is very, very misleading and a source for error and
> frustration.
>
> Are there any considerations for future versions of ImageJ
> to make this behavior optional?
> I'd really like to use 16-bit grayscale PNG.
>
> Where can I submit a bug report? (I'm serious)
>
>> -wayne
>>
>
>
> //
> Best regards,
>
> G. Jehle
>
> HTWG Konstanz - University of Applied Sciences
> Fakultät Informatik
>
> HTWG Konstanz
> Fakultät Informatik
> Brauneggerstraße 55
> D-78462 Konstanz
> www.htwg-konstanz.de
>