Page 1 of 1

Compress PNG image with Jmagick

Posted: 2007-11-16T04:08:58-07:00
by leelight
It is strange there is no block for Jmagick, so I post it here.

I have used Jmagick, no doubt it is a nice tool, but I have problem with compressing the PNG image.
Now I want to reduce the size of my png image, for example from 20kb to under 10kb, but with the best quality.

Now I want to use Jmagik to compress the image, and I have tried every way to do that, I can reduce the size of image, but the quality is so urgly that I can not use it.
This is my java code, could you give me some advice how to reduce the size but with the best quality??

scale=50;

ImageInfo info = new ImageInfo("input.png");
MagickImage image = new MagickImage(info);
Dimension orgDim = image.getDimension();
width = (int) orgDim.getWidth();
height = (int) orgDim.getHeight();
System.out.println("orginal Colors: " + image.getNumberColors());
System.out.println("orginal Depth: " + image.getDepth());
System.out.println("orginal Colorspace: " + image.getColorspace());
System.out.println("orginal Total colors " + image.getTotalColors());
System.out.println("orginal Resolution units: " + image.getUnits());


// clear all the metadata
image.profileImage("*", null);
image.setImageFormat("PNG");

/*
* If I use the scaleImage here, I can get one small image, but the quality is worse.
*/
//image = image.scaleImage(width*scale/100, height*scale/100);
//image = image.scaleImage(width, height);


image.setDepth(1);

QuantizeInfo quantizeInfo = new QuantizeInfo();
quantizeInfo.setColorspace(ColorspaceType.CMYKColorspace);//XYZColorspace
quantizeInfo.setNumberColors(8);
quantizeInfo.setTreeDepth(1);
quantizeInfo.setColorspace(0);
quantizeInfo.setDither(0);

image.quantizeImage(quantizeInfo);

System.out.println("last Colors: " + image.getNumberColors());
System.out.println("last Depth: " + image.getDepth());
System.out.println("last Colorspace: " + image.getColorspace());
System.out.println("last Total colors " + image.getTotalColors());
System.out.println("last Resolution units: " + image.getUnits());

// TODO justify to write in cache file
image.setFileName("output.png");
System.out.println("write Image: " + "output.png");
image.writeImage(info);


Could you help me that?? waiting for your reply!!!!

Re: Compress PNG image with Jmagick

Posted: 2007-11-19T07:11:32-07:00
by leelight
Anyone knows?? help~~

Re: Compress PNG image with Jmagick

Posted: 2007-11-19T08:23:33-07:00
by Bonzo
I know nothing about jmagick but with Imagemagick if you use thumbnail instead of resize it strips out any image data e.g. EXIF data. You can also use -strip to remove the data.

I would look for something similar in Jmagick.

Re: Compress PNG image with Jmagick

Posted: 2007-11-21T06:18:48-07:00
by leelight
Thanks,
I have tried thumnail, but the quality is so ugly, I can not take it.......
The Exif data I have remove it.

Re: Compress PNG image with Jmagick

Posted: 2007-11-22T17:12:19-07:00
by anthony
The thumbnail is basically equivelent to
-strip any and all profiles from the final image
-scale to 5 time final size (if that shrinks the image -- this is for speed not quality
-resize image to the final size.

Now -resize is acceptable to you, so just add a -strip type operator, that should not mody the image data further, unless the images profile was not in RGB format!

Final solution. change the image color profile, strip them, resize image.
You can resize first if you really want.