Auto-detect optimal image output format, How to

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
rdkill

Auto-detect optimal image output format, How to

Post by rdkill »

I am using ImageMagick on a website to process images of scanned books. Text-only pages look the best as GIF/PNG; and mostly-image-filled pages look the best as JPEG. I'd like to find the optimal output format on the fly.

Any recommendations on how to approach this? I would imagine I need to look at the separation of colors to see how badly I need gradients (which JPEG would be ideal for). I'm looking to maximize quality and minimize file size, since they are being served on the web. The easiest solution would be really high quality JPEGs, but I'd really like to optimize the viewing experience.

Possible cases:
1) A full-page color graphic
2) Grayscale text on a FFF background
3) Text on a brown-ish background (likely several shades of brown)
4) Half text, half grayscale graphics
and so on and so on...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Auto-detect optimal image output format, How to

Post by snibgo »

I suggest that subjective "quality" is hard to determine in software, including ImageMagick.

You might find that creating both PNG and JPG versions, with suitable "-quality" settings, then choosing the smallest of the two, gets you somewhere near.
snibgo's IM pages: im.snibgo.com
rdkill

Re: Auto-detect optimal image output format, How to

Post by rdkill »

I see. That makes sense. After picking JPEG/PNG, would you recommend sticking to that format for every generated size? The generated images range from a width of ~1100px down to 82px for the smallest zoom.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Auto-detect optimal image output format, How to

Post by snibgo »

You'll have to do trials, trading off download size, visual appearance and programming complexity.

In my experience, most scanned pages compress best in jpg, even the most graphic kinds. (But your scans may be different.)
snibgo's IM pages: im.snibgo.com
rdkill

Re: Auto-detect optimal image output format, How to

Post by rdkill »

I have been using JPEG, but I realized that it's less than optimal for some pages such as gray-scale text-only ones. JPEG needs to be pretty high quality to prevent artifacts, but PNG8 would look almost perfect using 8 colors in less than half the size of the JPEG. I wouldn't care if I wasn't serving for the web, but I'm trying to make this thing really quick and responsive.

I'm hoping I can find some PNG and JPEG settings that will consistently produce good quality results, then simply tell the script to keep the one that's smaller.

*begins testing
Post Reply