convert - downscaling comic pages

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?".
steve___
Posts: 28
Joined: 2012-07-17T09:52:07-07:00
Authentication code: 15

convert - downscaling comic pages

Post by steve___ »

I'll have users uploading jpg's, png's and possibly gif's. If the width
is greater than 750px (eg) then I need to resize them. See
https://dev.zcomix.com for examples of images I'll be getting.

I'd like to keep the image quality high, as well as the logic simple. I
would never be upscaling the images, only downscaling.

I'm not sure which convert command(s) to use. Does something like this
make sense:

$ convert in.png -colorspace Lab -interpolate catrom -set colorspace Lab -colorspace sRGB out.png

Without scaling, my test image went from 13K to 28K.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert - downscaling comic pages

Post by fmw42 »

It is very subjective what filter and colorspace to use. One trades one kind of quality vs another or one kind of artifact vs another. See
http://www.imagemagick.org/Usage/resize/
http://www.imagemagick.org/Usage/filter ... downsample

Personally I like the sharpness of Catrom and can tolerate other artifacts. But others like Nicolas, prefer other less sharp filters. I am not sure if Lab is better than RGB. My eyes don't see enough difference whether doing it in linear or non-linear colorspaces. But I may be more tolerant than others.

You will need to add -resize "750>" to your command and -filter rather than -interpolate. This will only resize if one or the other dimension is larger than 750.

Code: Select all

convert in.png -colorspace Lab -filter catrom -resize "750>" -set colorspace Lab -colorspace sRGB out.png
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: convert - downscaling comic pages

Post by NicolasRobidoux »

If I understand correctly, what Steve wants to do is resize, mostly, line art (not pixel art, line art).
My general recommendations for downsampling (linked above by Fred) consequently may or may not "hit the spot".
Although I can make some educated guesses, this falls outside of what I have a lot of experience downsampling well.
He also raises the issue that he'd like the file sizes to be small (expressing, for example, surprise that doing what in principle should be a "no-op" increases file size). This last topic I do not want to address, because interacting well with compression is something that until recently was my bread and butter, and a robust high quality resize/recompress toolchain is something that requires quite a bit of technical know how to assemble, let alone install/use. So I won't be talking about that much.
------
Given that it is line art, my very first recommendation is that Steve try vipsthumbnail http://libvips.blogspot.dk/2013/11/tips ... bnail.html which I believe now has a well put together node.js wrapper https://github.com/lovell/sharp. It is not as flexible or high quality as what one can do with ImageMagick (alone or combined with other tools), but it should be a good first pass given your source material.
(Daughter wants computer to use google translate to write a BDay card in Danish. I'll add a bit more to this later.)
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: convert - downscaling comic pages

Post by NicolasRobidoux »

Another good start: http://www.imagemagick.org/Usage/resize/#thin_lines.
A bit more info RE: http://www.imagemagick.org/Usage/filter ... downsample.
With line art, the "downsample through linear light" diktat may or may not be relevant. Downsampling through Lab, which normally I would absolutely not recommend, may be OK. When it's "monochrome" line art, sigmoidization may work well as well. I'd try the linear light toolchain first, though. Just don't get married to it without testing. But realize that not using linear light can give unexpected results.
Many of the recommended filters will introduce quite a bit of "ringing". Most likely, you don't want that.
When the downsampling ratio is not too large, my first pass would be linear light EWA Robidoux (the default -distort Resize filter), linear light EWA Quadratic, EWA RobidouxSoft viewtopic.php?f=22&t=19823&start=30#p109820 possibly combined with contrast manipulation of the final result http://www.imagemagick.org/Usage/color_mods/#sigmoidal and, if you don't mind haloing (and possibly use sigmoidization or another color space than linear RGB) linear light EWA LanczosSharpest viewtopic.php?f=2&t=25268
-----
My apologies for the terse and untested answer. This is all I have the time for these days.
Good luck! Please let us know what you settle on, and why.
-----
One final comment: Please note that png can use drastically different types of compression http://www.imagemagick.org/Usage/formats/#png_quality. Most likely, this explains why you're no-op operation increases the file size quite a bit. But there are a number of other issues that affect the final file size. pngcrush (to which Glenn, one of the ImageMagick developers, contributed) can help keeping png sizes under control.
Really, however, your script should check if the input and output width and height are the same, and pick the original if it's smaller than the final result and in a format you're happy with.
Last edited by NicolasRobidoux on 2014-05-25T23:01:20-07:00, edited 3 times in total.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: convert - downscaling comic pages

Post by NicolasRobidoux »

P.S. RE: last comment:
This is what the boldface text in http://www.imagemagick.org/Usage/resize/#resize hints at.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: convert - downscaling comic pages

Post by NicolasRobidoux »

When dealing with line art, there is one good reason to use Lab (or L*a*b*): There is a luma/luminance channel.

When downsampling, however, it is >>generally<< preferable to use linear light.

A good way to get both a color space with a luma/luminance channel and linear light is resampling through XYZ instead of (linear) RGB. This is trivial to do in ImageMagick.

The Y channel is a decent luminance channel.

-----

Warning: All my advice here is off the top of my head combining rules of thumb verified in other contexts. Don't use blindly.

And, until you've checked that the results are close, I'd stick to an HDRI version of ImageMagick if you do "unusual" color space transformations or use non-EWA filters with negative lobes (if you use -resize instead of -distort Resize).
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: convert - downscaling comic pages

Post by NicolasRobidoux »

It would be nice if someone took the time to do a comparison like viewtopic.php?f=22&t=21804 but downsampling line art instead of upsampling.
(Tensor) Lanczos

Code: Select all

-filter Lanczos -resize
, EWA Robidoux

Code: Select all

-distort Resize
or the above mentioned EWA LanczosSharpest 4-lobe would be my first picks. (Tensor Ginseng, like in the upsampling series of comparisons, would be a fine first pass too.)
Unfortunately, "someone" can't be me these days.
I also suspect that if images are downsampled by more than about 2x or 3x, something will have to be done to prevent the "lines" from being blurred away.
Last edited by NicolasRobidoux on 2014-05-31T04:40:50-07:00, edited 3 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert - downscaling comic pages

Post by fmw42 »

Makes sense for the OP to do that and would be nice if the results were reported back here.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: convert - downscaling comic pages

Post by NicolasRobidoux »

With line or pixel art, downsampling through Lab may actually be an excellent choice (untested: off the top of my head).

Rationale: It has a luma channel, and if the line art is mostly made with dark lines, the "dark" bias that one gets going through perceptual may help keeping dark lines from being blurred away.
steve___
Posts: 28
Joined: 2012-07-17T09:52:07-07:00
Authentication code: 15

Re: convert - downscaling comic pages

Post by steve___ »

I'd like to take a stab at doing some tests. How do I access the EWA
filters? I do not see them in the output of 'convert -list filter'.

Before comparing the output, I'd like to get feedback on the syntax I'm
using to be sure it's what Nicolas meant above.

Code: Select all

convert img/bw-thin_line.jpg -colorspace RGB -filter Lanczos -resize x2549 -colorspace sRGB lanczos-rgb-bw-thin_line.jpg
convert img/bw-thin_line.jpg -colorspace RGB -filter Robidoux -distort Resize 70.8% -colorspace sRGB robidoux-rgb-bw-thin_line.jpg
convert img/bw-thin_line.jpg -colorspace RGB -filter Lanczos2Sharp -define filter:lobes=4 -resize x2549 -colorspace sRGB lanczos2sharp-rgb-bw-thin_line.jpg
convert img/bw-thin_line.jpg -colorspace RGB +sigmoidal-contrast 7.5 -define filter:window=Quadratic -distort Resize 70.8% -sigmoidal-contrast 7.5 -colorspace sRGB ginseng-rgb-bw-thin_line.jpg

convert img/bw-thin_line.jpg -colorspace Lab -filter Lanczos -resize x2549 -colorspace sRGB lanczos-lab-bw-thin_line.jpg
convert img/bw-thin_line.jpg -colorspace Lab -filter Robidoux -distort Resize 70.8% -colorspace sRGB robidoux-lab-bw-thin_line.jpg
convert img/bw-thin_line.jpg -colorspace Lab -filter Lanczos2Sharp -define filter:lobes=4 -resize x2549 -colorspace sRGB lanczos2sharp-lab-bw-thin_line.jpg
convert img/bw-thin_line.jpg -colorspace Lab +sigmoidal-contrast 7.5 -define filter:window=Quadratic -distort Resize 70.8% -sigmoidal-contrast 7.5 -colorspace sRGB ginseng-lab-bw-thin_line.jpg
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert - downscaling comic pages

Post by fmw42 »

EWA is of no special help unless you are distorting to some drastically changed view like an extremely tilted perspective. Face-on view don't need EWA.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: convert - downscaling comic pages

Post by NicolasRobidoux »

fmw42 wrote:EWA is of no special help unless you are distorting to some drastically changed view like an extremely tilted perspective. Face-on view don't need EWA.
I beg to disagree: The best resampling filters in ImageMagick are EWA methods. So, it you want the best possible results, you "need" EWA.
To resize using EWA use

Code: Select all

-distort Resize
instead of

Code: Select all

-resize
That's it.
See http://www.imagemagick.org/Usage/filter/nicolas/ and (bleeding edge) http://www.luminous-landscape.com/forum ... ic=91754.0, which discusses a successor to sigmoidization (which may still be quite good with line drawings, but most likely not as good as "halo reduction by luminance weighted gamma blending").
P.S. Fred: I think we misunderstood each other.
Last edited by NicolasRobidoux on 2014-07-31T23:11:24-07:00, edited 2 times in total.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: convert - downscaling comic pages

Post by NicolasRobidoux »

If you are downscaling and you have thin lines, I suggest you try something like

Code: Select all

convert input.png -set colorspace sRGB -colorspace RGB -gamma GAMMA -distort Resize SIZE_SPEC -gamma RECIPROCAL_OF_GAMMA -colorspace sRGB ...
as a first pass.
Going through a gamma space thickens (thin) lines at one end or the other of the lightness spectrum.
steve___
Posts: 28
Joined: 2012-07-17T09:52:07-07:00
Authentication code: 15

Re: convert - downscaling comic pages

Post by steve___ »

How do I know which settings to use for GAMMA and RECIPROCAL_OF_GAMMA?

The thin-line artwork is one of 12 images to test, some of the images
have full colour artwork.

I have done some initial tests. I will post the results later.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: convert - downscaling comic pages

Post by NicolasRobidoux »

Start by trying GAMMA = 3 and RECIPROCAL_OF_GAMMA = .333333333333333.
The reverse them (GAMMA = .3333333333333 etc).
This should give a pretty good idea of the effect.
GAMMA = 1 and RECIPROCAL_OF_GAMMA = 1 is wasting cycles to do nothing.
Note that if your IM is only 8 bit, this will produce garbage, most likely. Make sure you are Q16 or HDRI.
Post Reply