Page 1 of 1

PNG and image size reduction

Posted: 2009-10-05T07:53:45-07:00
by pabell4
Hi,
I am trying to use ImageMagick Convert to process some black/white line drawings and have spotted something I'm hoping that can be resolved easily.
We have starting images which are normally 440x440 pix in size and have transparent backgrounds - e.g.:

Image

I then use IM to crop and add a transparent border (parameters: -trim -bordercolor none -border 5) and the file size drops a little - giving this image:

Image

If I then try and reduce the file size by specifying the -colors and smoothing using +dither (parameters: -trim -bordercolor none -border 5 +dither -colors 16), the file size does almost half. What does happen though is the image made has a pale grey shaded background:

Image

Am I including the wrong parameters or putting them in the wrong order? If I try and specify -background none then I get no change to the last image. If I lower the number of colors to 8 the shading gets darker (but the image size and quality does go down).

Any pointers appreciated. I'd like to keep the smaller file size but remove the shading.
Thanks in advance.

Wyn

Re: PNG and image size reduction

Posted: 2009-10-05T12:48:37-07:00
by fmw42
I don't believe that you can affect the color choice when using -colors unless you prepare a color map to be used. See -map

http://www.imagemagick.org/Usage/quanti ... _not_exact
http://www.imagemagick.org/Usage/quantize/#map
http://www.imagemagick.org/Usage/quantize/#web_safe

Re: PNG and image size reduction

Posted: 2009-10-05T23:01:52-07:00
by anthony
The -colors tens not to pick extreme colors. That includes transparency. See...
http://www.imagemagick.org/Usage/quanti ... _not_exact

Using -normalize on the alpha channel may fix the problem.

My suggestion....
Flatten the trimmed image onto white to make a grayscale image.
Now try -colors 16 or -ordered-dither threshold,16 to reduce the number colors.

The first trys to find the 16 best colors, the latter 16 linearly distributed colors.
You could also try removing gamma do the ordered dither and restore gamma too,

Once you have the colors reduced and you really have a pure white and pure black colors (-normalize if need be) then use -background black -alpha shape

Re: PNG and image size reduction

Posted: 2009-10-07T04:32:14-07:00
by pabell4
Hi - many thanks for the suggestion. It's getting there but not quite.

I took your advice and flattened, trimmed, set white border and the colors [parameters: -flatten -trim -bordercolor white -border 5 -colors 16] to hopefully get the pure black/white image. The size is reduced and quality is good:

Image

I then tried to "use -background black -alpha shape" from your reply [parameters: -flatten -trim -bordercolor white -border 5 -colors 16 -background black -alpha shape] and get the following:

Image

How do I re-enable the transparent background option to the earlier image - my attempts to set -background to none did not change the first image. Looking at the examples pages I'm confused by the matte, alpha and tranparency mask suggestions.

I'm doing this all in one IM command against the original image. Do you suggest I split this into different IM tasks?
Sorry if I have misunderstood your instructions, and thanks for your help so far.

Wyn

Re: PNG and image size reduction

Posted: 2009-10-07T10:45:37-07:00
by fmw42
try [parameters: -flatten -trim -bordercolor white -border 5 -colors 16 -channel rgba -matte -transparent white]

Re: PNG and image size reduction

Posted: 2009-10-07T17:30:47-07:00
by anthony
the border will do the flatten! but better to extract the alpha!
For -colors in this example I recommend also adding +dither

Code: Select all

 -trim -alpha extract -bordercolor black -border 5 \
 +dither -colors 16 -normalize \
 -background black -alpha shape
or using ordered dither (linearly selected range of colors)

Code: Select all

-trim -alpha extract -bordercolor black -border 5 \
-ordered-dither threshold,16   -background black -alpha shape
or gamma offset ordered dither

Code: Select all

 -trim -alpha extract -bordercolor black -border 5 \
 -gamma 2 -ordered-dither threshold,16 -gamma 0.5 \
-background black -alpha shape
This generates non-linear color range which (for the above image) generates a slightly better result, for the color reduction. This works well for '-colors' too.

Re: PNG and image size reduction

Posted: 2009-10-08T02:59:35-07:00
by pabell4
hi again - and many thanks to you both for replying. We're getting closer!

I tried fmw42's parameter list. It produces an image of good quality but does not have the transparent background:

Image

I then tried Anthony's options. Of the 3 - the first one where the alpha is extracted [parameters: -trim -alpha extract -bordercolor black -border 5 +dither -colors 16 -normalize -background black -alpha shape] gives an image of reduced file size, good quality and thankfully a transparent background:

Image

The other two suggestions produced images of comparable size but have lost some of the fine details of the image like the fine hashed lines and some text - which were retained in the image above.

All looks fine if I view the image in Firefox, Opera, Google Chrome and Safari, but it looks like a solid black box if viewd with IE6. I can see that there are issues with IE6 and tranparent pngs, but my original image is a tranparent png and that is fine. Can anyone shed anything on what IM is doing to the image so that it no longer can be viewd in IE?
I can see from web searches it looks to be fixed with IE 7 and later. I'll check on that. People have also written scipts to apply to each page to get the images to display, but I'd rather not go down that route if possible.
Could it be my IM version? I'm running version ImageMagick-6.4.4 - running on Sun Solaris.

Many thanks for your continued help,
Wyn

Re: PNG and image size reduction

Posted: 2009-10-12T02:02:04-07:00
by pabell4
Good morning,

Has anyone had any thoughts on the IE 6 png display issue I mentioned last week?
I have checked IE 7/8 and many other browsers and all is fine - it's just an issue with IE 6.

Could the initial bit depth of the original png be an issue?
I'm going to play around with 24/32 bit settings in case that helps, but if anyone has any other ideas that would be really helpful.

Many thanks,
Wyn

Re: PNG and image size reduction

Posted: 2009-10-12T16:47:31-07:00
by anthony
PNG in IE6 problem is with transparency. If you have no transparency, then you have no issue. If the web page it is being displayed on does not have a fancy multiple color background, then you can just set the colors appropriatally.

Instead of doing the -background black -alpha shape use +level-colors DodgerBlue,black instead, replacing DodgerBlue with whatever the background color of the page is. The resulting image will not have an alpha.

Re: PNG and image size reduction

Posted: 2009-10-13T02:04:09-07:00
by pabell4
Hi Anthony,

Again - many thanks for the feedback. I appreciate the issue here is with IE6 transparency, and at least I have a working method to use when the IE6 browser stats ratio tails off now that users are slowly migrating to IE8.

It may help to provide a little background. Our current production method is done on a PC - where a transparent gif is created, and then scaled, cropped and bordered with Eyebatch, and finally gif2png is used to make a transparent png that can be seen in IE6 and all other browsers.

I am trying to replicate this on a unix server - I can create a transparent starting png image and using IM to crop, border, scale and reduce the color depth.

I'm still getting the images at about 50% bigger in file size than those done using the PC method, and that way still retains the tranparency in a form we can use on IE6. But like you say I can tailor the background color as it is not fancy in the application, and the benefits to the production process that no longer needs to run externally is a major plus.

Thanks again for all of your help,
Wyn

Re: PNG and image size reduction

Posted: 2009-10-13T18:30:34-07:00
by anthony
I believe IE6 will user a colortabled version of PNG which whould be smaller.
However while IM can produce PNG8 with a color table it only does so with
GIF like boolean transparency, even though PNG can have semi-transparent colors in the color table.

For Linux you may need to go to one of the other PNG specific programs to get a colortable (quantized) PNG with semi-transparency. Such as though listed in...
http://www.imagemagick.org/Usage/formats/#png_non-im

Try for example, pngnq
http://www.cybertherial.com/pngnq/pngnq.html

No guarantees though.