Page 1 of 1
size of .jpg images, information theory
Posted: 2010-06-24T06:35:28-07:00
by coulon
When I convert a color image to monochrome with the -monochrome or -threshold option the size of the result is bigger than the source image. AFAIK the information coded in three bytes is reduced in one bit in this process. Should I add a -compress option to optain a size reduction?
Both source and result images are in .jpg format.
Re: size of .jpg images, information theory
Posted: 2010-06-24T11:12:47-07:00
by Drarakel
Greyscale images can be stored with 8 bits/pixel in JPG. This can decrease the filesize, but not as much as you would expect. Monochrome images (or better: images where dithering was applied) however won't always give low filesizes, I think. The 'speckles' that are added by the dithering and the hard contrasts are not optimal for the JPG compression. The '-compress' option won't change a thing, because it's always JPG compression in JPG files.

But you can change the filesize in JPG with options like '-quality', '-sampling-factor' and even '-interlace'. Also, stripping the metadata can decrease the filesize. ImageMagick sometimes can take the values for quality and sampling-factor from the input JPG. The default values in the current IM version are quality 92 and sampling-factor 2x2, I think.
You could consider using another format for monochrome images. PNG will do better (and is even lossless).
If you need more specific tips, then post your input (and output) files.
Re: size of .jpg images, information theory
Posted: 2010-06-24T11:18:31-07:00
by snibgo
In addition to Drarakel's post, try:
identify -verbose x.jpg
on your output fuile. You might find that many pixels are not quite black or white. This is an artefact of jpeg compression, and is graylevel so requires one byte per pixel. You might find that png compresses bilevel images better:
convert in.jpg -threshold 50% out.png
Still graylevel, but probably smaller than jpeg.
Re: size of .jpg images, information theory
Posted: 2010-06-24T12:20:52-07:00
by coulon
snibgo wrote:In addition to Drarakel's post, try: [...]
convert in.jpg -threshold 50% out.png
This solves my size problem. Thanks!
Re: size of .jpg images, information theory
Posted: 2010-06-24T16:28:06-07:00
by anthony
snibgo wrote:You might find that png compresses bilevel images better:
convert in.jpg -threshold 50% out.png
Still graylevel, but probably smaller than jpeg.
IM may not produce the smallest PNG. Especially for bilevel You may like to also include
-define png:bit-depth=1
-define png:color-type=3
Which should ensure you end up with a better data format before compression even starts.
See IM Exmaples, Common File Formats, PNG, Write Settings
http://www.imagemagick.org/Usage/formats/#png_write
Alternativally if it is just for storage you could use image.pbm.gz
PBM is a much simpler binary bitmap format, which with gzip should compress very well, though it is not a commonly understood file format.
Re: size of .jpg images, information theory
Posted: 2010-06-24T17:13:51-07:00
by Drarakel
I don't want to interrupt every thread with the PNG problems..
But I would be cautious with these options at the moment:
anthony wrote: -define png:bit-depth=1
-define png:color-type=3
Example:
convert -size 17x8 xc:white xc:black -append -depth 8 -define png:bit-depth=1 -define png:color-type=3 test.png
I get a crash.

But maybe, it's ok again in the next IM version.
@coulon: At the moment, I would just output it with the default settings of ImageMagick and pass that file to a PNG optimizing tool like pngout - if smallest filesize is needed. (Also covered in the
documentation.)