ImageJ resizing PNG with no transparency loss - how to

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

ImageJ resizing PNG with no transparency loss - how to

Martin Asenov



Hi, guys,


Congratulations for the awesome framework - works fast and results in good
quality.


However, I tried to resize a PNG image with some transparency, and
unfortunately, the transparency got lost and replaced with the black color.



Here is my small snippet of code with which I do the resizing:


 public BufferedImage resizeImage(BufferedImage image, int maxWidth) throws
IOException {
 ImagePlus ip = new ImagePlus();
 ip.setImage(image);


 ImageProcessor imp = ip.getProcessor();
 ImageProcessor imp2 = imp.resize(maxWidth);


 ImagePlus ip2 = new ImagePlus("Resized Image", imp2);


 return ip2.getBufferedImage();
 }


It works great for JPEG, but when it comes to PNG transparency, it doesn't
do the thing I need. I was unable to find a work around in the ImageJ API.



Would you please assist?


Thanks a lot in advance!


Martin
Reply | Threaded
Open this post in threaded view
|

Re: ImageJ resizing PNG with no transparency loss - how to

Jarek Sacha-2
Martin:

Below is scaling that works with transparent image using standard Java
API (Groovy syntax).

Jarek
http://ij-plugins.sf.net/

------------------------------------------------------------------------

import java.awt.RenderingHints
import java.awt.image.BufferedImage
import javax.imageio.ImageIO

// Read image to scale
def src = ImageIO.read(new File("test/data/scala64.png"))

// Create scaled destination image with transparency (ARGB)
int destWidth = 32
int destHeight = 32
def dest = new BufferedImage(destWidth, destHeight,
BufferedImage.TYPE_INT_ARGB);

// Paint source image into the destination, scaling as needed
def graphics2D = dest.createGraphics()
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR)
graphics2D.drawImage(src, 0, 0, destWidth, destHeight, null)
graphics2D.dispose()

// Save destination image
ImageIO.write(dest, "PNG", new File("test/data/scala32.png"))

------------------------------------------------------------------------

On 5/3/2011 6:27 AM, Martin Asenov wrote:
> However, I tried to resize a PNG image with some transparency, and
> unfortunately, the transparency got lost and replaced with the black color.
Reply | Threaded
Open this post in threaded view
|

Re: ImageJ resizing PNG with no transparency loss - how to

Martin Asenov



Hi, Jarek


Thanks for your output. As far as I understand, ImageJ does not currently
support resizing PNG images with no transparency loss, is this right?


Thank you,


Martin


----- Цитат от Jarek Sacha ([hidden email]), на
04.05.2011 в 04:55 -----   Martin:


Below is scaling that works with transparent image using standard Java
API (Groovy syntax).


Jarek
http://ij-plugins.sf.net/


------------------------------------------------------------------------


import java.awt.RenderingHints
import java.awt.image.BufferedImage
import javax.imageio.ImageIO


// Read image to scale
def src = ImageIO.read(new File("test/data/scala64.png"))


// Create scaled destination image with transparency (ARGB)
int destWidth = 32
int destHeight = 32
def dest = new BufferedImage(destWidth, destHeight,
BufferedImage.TYPE_INT_ARGB);


// Paint source image into the destination, scaling as needed
def graphics2D = dest.createGraphics()
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR)
graphics2D.drawImage(src, 0, 0, destWidth, destHeight, null)
graphics2D.dispose()


// Save destination image
ImageIO.write(dest, "PNG", new File("test/data/scala32.png"))


------------------------------------------------------------------------


On 5/3/2011 6:27 AM, Martin Asenov wrote:
   However, I tried to resize a PNG image with some transparency, and
unfortunately, the transparency got lost and replaced with the black color.
     



Reply | Threaded
Open this post in threaded view
|

Re: ImageJ resizing PNG with no transparency loss - how to

Datarunner
Or at least it would be good to know if it's possible to change the black color to white, because most of the transparent images would look better in that color. Besides that, still looking for a solution to this, so if you've got experience with ImageJ and would be willing to create a solution to the original question, I would greatly appreciate it.
Thank you very much
Oliver