Page MenuHomePhabricator

ImageMagick thumbnails have moiré problems
Open, LowPublic

Description

We use a lot of historic media, for obvious reasons. Unfortunately, engravings, a very common type, often have large numbers of not-quite-horizontal lines.

This results in bad thumbnailing at times. For example:

http://upload.wikimedia.org/wikipedia/commons/thumb/1/16/Rajpoots_2.png/310px-Rajpoots_2.png

is not a very good thumbnail of http://upload.wikimedia.org/wikipedia/commons/1/16/Rajpoots_2.png

Is there anything that can be done?


Version: unspecified
Severity: normal

Details

Reference
bz24889

Event Timeline

bzimport raised the priority of this task from to Low.Nov 21 2014, 11:03 PM
bzimport set Reference to bz24889.
bzimport added a subscriber: Unknown Object (MLST).

Note that image may be a good test: MOST thumbnails have some sort of Moiré in the sky.

Sure, if we start scaling each image by hand, picking the best algorithm.

All is a trade off between speed, quality and sharpness. And the bigger the image, the worse the effects can be of course.

See also: http://www.imagemagick.org/Usage/resize/#filter

Specifically: http://www.imagemagick.org/Usage/resize/#best_filter

Ah, well. Figured it was going to be something like that. ~~~~

http://commons.wikimedia.org/wiki/File:Rajpoots_2-scaled.tif

Another example for people who will want to work on this and test scaling routines.

Created attachment 9065
Sample scale-downs using ImageMagick & gimp

Adding a Gaussian blur on the original image before scaling seems to work wonders for this engraving; a 10x10 blur in Gimp for 310px or a 2x2 blur for 1600px both come out looking fairly smooth.

Blurring may however be relatively slow, especially on large images; it's probably not the best idea to turn it on as a default.

However.... I notice I actually get very good results using ImageMagick's '-resize' mode instead of '-thumbnail'. By default -resize applies a Mitchell filter; I can also apply '-filter Gaussian' which looks not quite as sharp.

The -filter option seems to be ignored by -thumbnail; or if it is applied it looks like it happens after a much more primitive resampling which'll be what gives us the heavy moire:

http://www.imagemagick.org/Usage/resize/#thumbnail

Of course part of the reason we use -thumbnail is probably because it's fast because it doesn't do all that filtering at full resolution. :P

Being able to flip particular images into the slower, more filtered scaling may actually be useful -- I suspect a simple on/off will be sufficient. But I don't know what performance impact it'll have; very large source images with high contrast are the target group, but very large source images are the exact ones that are slow and use lots of memory to resize. :(

Attached:

engraving-wtf.png (540×700 px, 266 KB)

Created attachment 9589
Some frequency spectra

A comparison showing the issue in the frequency domain.

The left image is the frequency spectrum of the source (VIPS im_fwfft + im_scaleps). Note the strong peaks on the X and Y axes representing the engraving pattern.

The middle image has a 10px block average applied, like what VIPS im_shrink uses. Note the notches in the response which suppress part of the engraving pattern.

The right image used a 10px radius Gaussian filter like what Brion used in one of his comparison images. Note the circular symmetry in the central area and rapid falloff out of the pass band.

If you're reducing the image far enough that the engraving pattern will generate Moire fringes, you need to use a filter which will strongly attenuate the part of the frequency spectrum where the engraving pattern is located.

Attached:

join-false-310.png (236×930 px, 206 KB)