size of .jpg images, information theory

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
coulon

size of .jpg images, information theory

Post 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.
Last edited by coulon on 2010-06-24T21:18:55-07:00, edited 1 time in total.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: size of .jpg images, information theory

Post 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. :wink:
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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: size of .jpg images, information theory

Post 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.
snibgo's IM pages: im.snibgo.com
coulon

Re: size of .jpg images, information theory

Post by coulon »

snibgo wrote:In addition to Drarakel's post, try: [...]
convert in.jpg -threshold 50% out.png
This solves my size problem. Thanks!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: size of .jpg images, information theory

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: size of .jpg images, information theory

Post 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. :wink:
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.)
Post Reply