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.
size of .jpg images, information theory
size of .jpg images, information theory
Last edited by coulon on 2010-06-24T21:18:55-07:00, edited 1 time in total.
Re: size of .jpg images, information theory
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.

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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: size of .jpg images, information theory
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.
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.
snibgo's IM pages: im.snibgo.com
Re: size of .jpg images, information theory
This solves my size problem. Thanks!snibgo wrote:In addition to Drarakel's post, try: [...]
convert in.jpg -threshold 50% out.png
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: size of .jpg images, information theory
IM may not produce the smallest PNG. Especially for bilevel You may like to also includesnibgo wrote:You might find that png compresses bilevel images better:
convert in.jpg -threshold 50% out.png
Still graylevel, but probably smaller than jpeg.
-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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: size of .jpg images, information theory
I don't want to interrupt every thread with the PNG problems..
But I would be cautious with these options at the moment:
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.)
But I would be cautious with these options at the moment:
Example:anthony wrote: -define png:bit-depth=1
-define png:color-type=3
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.)