Compress PNG image with Jmagick
Posted: 2007-11-16T04:08:58-07:00
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!!!!
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!!!!