Compress PNG image with Jmagick

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
leelight

Compress PNG image with Jmagick

Post 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!!!!
leelight

Re: Compress PNG image with Jmagick

Post by leelight »

Anyone knows?? help~~
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Compress PNG image with Jmagick

Post 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.
leelight

Re: Compress PNG image with Jmagick

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Compress PNG image with Jmagick

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply