Manage PNG Transparency with ImageJ

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Manage PNG Transparency with ImageJ

Charles Flond
Hi All,

Does anybody know how to manage PNG files with transparency using ImageJ ? I
tried to use the function writeImageWithTransparency(ImagePlus imp, String
path, int transparentIndex) but it doesn't seem to work.

See http://rsb.info.nih.gov/ij/source/ij/plugin/PNG_Writer.java for more
details about this function.

Any suggestions would be welcomed ! Thanks in advance.

Charles
Reply | Threaded
Open this post in threaded view
|

Re: Manage PNG Transparency with ImageJ

Rasband, Wayne (NIH/NIMH) [E]
On Dec 27, 2010, at 12:06 PM, Charles Flond wrote:

> Hi All,
>
> Does anybody know how to manage PNG files with transparency using ImageJ ? I
> tried to use the function writeImageWithTransparency(ImagePlus imp, String
> path, int transparentIndex) but it doesn't seem to work.
>
> See http://rsb.info.nih.gov/ij/source/ij/plugin/PNG_Writer.java for more
> details about this function.
>
> Any suggestions would be welcomed ! Thanks in advance.

Plugins and scripts can save an 8-bit image as a PNG with transparency using

  imp = IJ.getImage();
  Prefs.setTransparentIndex(index);
  IJ.saveAs(imp, "PNG", file_path);

where 0<=index<=255. Macros can use

  call("ij.Prefs.setTransparentIndex", index);
  saveAs("PNG", file_path);

ImageJ does not support display with transparency so you will need to open the saved PNG in another application (e.g., a Web browser) to verify that the pixels with a value of 'index' are transparent. Note that PNG and GIF transparency only works with 8-bit images.

-wayne
Reply | Threaded
Open this post in threaded view
|

Re: Manage PNG Transparency with ImageJ

Charles Flond
Hello Wayne,

Thanks for your feedback. Is there a reason why PNG transparency is only
supported for 8-bit images ? Is it possible and / or plan to support
transparency for 16 / 32 bit images ?

Thanks in advance !

Charles

On Mon, Dec 27, 2010 at 10:49 PM, Rasband, Wayne (NIH/NIMH) [E] <
[hidden email]> wrote:

> On Dec 27, 2010, at 12:06 PM, Charles Flond wrote:
>
> > Hi All,
> >
> > Does anybody know how to manage PNG files with transparency using ImageJ
> ? I
> > tried to use the function writeImageWithTransparency(ImagePlus imp,
> String
> > path, int transparentIndex) but it doesn't seem to work.
> >
> > See http://rsb.info.nih.gov/ij/source/ij/plugin/PNG_Writer.java for more
> > details about this function.
> >
> > Any suggestions would be welcomed ! Thanks in advance.
>
> Plugins and scripts can save an 8-bit image as a PNG with transparency
> using
>
>  imp = IJ.getImage();
>  Prefs.setTransparentIndex(index);
>  IJ.saveAs(imp, "PNG", file_path);
>
> where 0<=index<=255. Macros can use
>
>  call("ij.Prefs.setTransparentIndex", index);
>  saveAs("PNG", file_path);
>
> ImageJ does not support display with transparency so you will need to open
> the saved PNG in another application (e.g., a Web browser) to verify that
> the pixels with a value of 'index' are transparent. Note that PNG and GIF
> transparency only works with 8-bit images.
>
> -wayne
>
Reply | Threaded
Open this post in threaded view
|

Re: Manage PNG Transparency with ImageJ

Rasband, Wayne (NIH/NIMH) [E]
On Dec 28, 2010, at 4:19 AM, Charles Flond wrote:

> Hello Wayne,
>
> Thanks for your feedback. Is there a reason why PNG transparency is only
> supported for 8-bit images ? Is it possible and / or plan to support
> transparency for 16 / 32 bit images?

ImageJ ignores the alpha values in ARGB images so what you need to do is set the alpha values, create an ARGB BufferedImage from the int[] pixel array, and then write the BufferedImage as a PNG using

    ImageIO.write(bufferedImage, "png", new File(path));

You can't use ImagePlus.getBufferedImage() because it returns an RGB BufferedImage.

-wayne

> On Mon, Dec 27, 2010 at 10:49 PM, Rasband, Wayne (NIH/NIMH) [E] <
> [hidden email]> wrote:
>
>> On Dec 27, 2010, at 12:06 PM, Charles Flond wrote:
>>
>>> Hi All,
>>>
>>> Does anybody know how to manage PNG files with transparency using ImageJ
>> ? I
>>> tried to use the function writeImageWithTransparency(ImagePlus imp,
>> String
>>> path, int transparentIndex) but it doesn't seem to work.
>>>
>>> See http://rsb.info.nih.gov/ij/source/ij/plugin/PNG_Writer.java for more
>>> details about this function.
>>>
>>> Any suggestions would be welcomed ! Thanks in advance.
>>
>> Plugins and scripts can save an 8-bit image as a PNG with transparency
>> using
>>
>> imp = IJ.getImage();
>> Prefs.setTransparentIndex(index);
>> IJ.saveAs(imp, "PNG", file_path);
>>
>> where 0<=index<=255. Macros can use
>>
>> call("ij.Prefs.setTransparentIndex", index);
>> saveAs("PNG", file_path);
>>
>> ImageJ does not support display with transparency so you will need to open
>> the saved PNG in another application (e.g., a Web browser) to verify that
>> the pixels with a value of 'index' are transparent. Note that PNG and GIF
>> transparency only works with 8-bit images.
>>
>> -wayne
>>
Reply | Threaded
Open this post in threaded view
|

Re: Manage PNG Transparency with ImageJ

ctrueden
In reply to this post by Charles Flond
Hi Charles,

Thanks for your feedback. Is there a reason why PNG transparency is only
> supported for 8-bit images ? Is it possible and / or plan to support
> transparency for 16 / 32 bit images ?
>

You can use the Bio-Formats Importer to read PNGs with transparency, with
the alpha channel as its own channel. You won't be able to visualize the
channel as a true alpha channel, but you will at least have access to all
the pixel values.

-Curtis

On Tue, Dec 28, 2010 at 3:19 AM, Charles Flond <[hidden email]> wrote:

> Hello Wayne,
>
> Thanks for your feedback. Is there a reason why PNG transparency is only
> supported for 8-bit images ? Is it possible and / or plan to support
> transparency for 16 / 32 bit images ?
>
> Thanks in advance !
>
> Charles
>
> On Mon, Dec 27, 2010 at 10:49 PM, Rasband, Wayne (NIH/NIMH) [E] <
> [hidden email]> wrote:
>
> > On Dec 27, 2010, at 12:06 PM, Charles Flond wrote:
> >
> > > Hi All,
> > >
> > > Does anybody know how to manage PNG files with transparency using
> ImageJ
> > ? I
> > > tried to use the function writeImageWithTransparency(ImagePlus imp,
> > String
> > > path, int transparentIndex) but it doesn't seem to work.
> > >
> > > See http://rsb.info.nih.gov/ij/source/ij/plugin/PNG_Writer.java for
> more
> > > details about this function.
> > >
> > > Any suggestions would be welcomed ! Thanks in advance.
> >
> > Plugins and scripts can save an 8-bit image as a PNG with transparency
> > using
> >
> >  imp = IJ.getImage();
> >  Prefs.setTransparentIndex(index);
> >  IJ.saveAs(imp, "PNG", file_path);
> >
> > where 0<=index<=255. Macros can use
> >
> >  call("ij.Prefs.setTransparentIndex", index);
> >  saveAs("PNG", file_path);
> >
> > ImageJ does not support display with transparency so you will need to
> open
> > the saved PNG in another application (e.g., a Web browser) to verify that
> > the pixels with a value of 'index' are transparent. Note that PNG and GIF
> > transparency only works with 8-bit images.
> >
> > -wayne
> >
>
Reply | Threaded
Open this post in threaded view
|

Re: Manage PNG Transparency with ImageJ

Jarek Sacha
In reply to this post by Charles Flond
Charles:

Below is the method that creates a BufferedImage from a ColorProcessor
and a ByteProcessor indicating transparency (both need to be of the same
size). This approach allow to vary transparency, that is, parts can be
semi-transparent.

You would use it like this:

final ColorProcessor cp = ...
final ByteProcessor alpha= ...
final BufferedImage bi = create(cp, alpha);
ImageIO.write(bi, "PNG", new File("transparent-image.png"));

Pixels with alpha equal 0 will be transparent, pixels with alpha=255
will be opaque, values between 0 and 255 will vary transparency.

public static BufferedImage create(final ColorProcessor src, final
ByteProcessor alpha) {
     if (src == null || alpha== null || src.getWidth() !=
alpha.getWidth() || src.getHeight() != alpha.getHeight()) {
         throw new IllegalArgumentException("Input parameters are not
valid: src=" + src + ", alpha=" + alpha);
     }

     final ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
     final int[] bits = {8, 8, 8, 8};
     final ColorModel cm = new ComponentColorModel(cs, bits, true,
false, Transparency.BITMASK, DataBuffer.TYPE_BYTE);
     final WritableRaster raster =
cm.createCompatibleWritableRaster(src.getWidth(), src.getHeight());
     final DataBufferByte dataBuffer = (DataBufferByte)
raster.getDataBuffer();

     final byte[] data = dataBuffer.getData();
     final int n = ((int[]) src.getPixels()).length;
     final byte[] r = new byte[n];
     final byte[] g = new byte[n];
     final byte[] b = new byte[n];
     final byte[] a = (byte[]) alpha.getPixels();
     src.getRGB(r, g, b);
     for (int i = 0; i < n; ++i) {
         final int offset = i * 4;
         data[offset] = r[i];
         data[offset + 1] = g[i];
         data[offset + 2] = b[i];
         data[offset + 3] = a[i];
     }

     return new BufferedImage(cm, raster, false, null);
}

Jarek

On 12/27/2010 12:06 PM, Charles Flond wrote:

> Hi All,
>
> Does anybody know how to manage PNG files with transparency using ImageJ ? I
> tried to use the function writeImageWithTransparency(ImagePlus imp, String
> path, int transparentIndex) but it doesn't seem to work.
>
> Seehttp://rsb.info.nih.gov/ij/source/ij/plugin/PNG_Writer.java  for more
> details about this function.
>
> Any suggestions would be welcomed ! Thanks in advance.
>
> Charles