Login  Register

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

Posted by Jarek Sacha-2 on May 04, 2011; 1:55am
URL: http://imagej.273.s1.nabble.com/ImageJ-resizing-PNG-with-no-transparency-loss-how-to-tp3683481p3683482.html

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.