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

Re: convert - downscaling comic pages

Post by snibgo » 2014-08-28T07:01:50-07:00

NicolasRobidoux wrote:(Hopefully I'm not suffering too badly from selection bias.)
You might do double-blind tests. (Technically, maybe these are merely single-blind.)

When your script has created images, it assigns a random number to each, and renames each with its number. The script keeps a log. You view and compare only the numbered files.

If you have, say, ten images, each processed by algorithm A and algorithm B, you compare the numbered images pair-wise. Then you look at the log file. If you always favour one algorithm over another, you know you are on to something. If it is 50-50, you might then determine the strengths and weaknesses of each.
snibgo's IM pages: im.snibgo.com

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 » 2014-08-28T09:06:49-07:00

Snibgo: Good idea. (Manana.)
P.S. Hopefully, Steve does not have selection bias.

steve___
Posts: 28
Joined: 2012-07-17T09:52:07-07:00
Authentication code: 15

Re: convert - downscaling comic pages

Post by steve___ » 2014-08-28T16:10:40-07:00

Is there a way to resize gif's so the file sizes does not become large?
Here are two files I'm testing with, 01.gif and 0304.gif.

01.gif GIF 3000x2008 3000x2008+0+0 8-bit sRGB 256c 962KB 0.000u 0:00.000
Lanczos2-ewa-gamma1.6-depth8-complex-cbz-01.png PNG 2390x1600 2392x1602+0+0 8-bit sRGB 2.972MB 0.000u 0:00.000

Here is a diff of 'identify -verbose':
http://sprunge.us/RHQd


0304.gif GIF 960x3211 960x3211+0+0 8-bit sRGB 16c 436KB 0.000u 0:00.000
Lanczos2-ewa-gamma1.6-depth8-complex-cbz-0304.png PNG 765x2560 767x2562+0+0 8-bit sRGB 1.839MB 0.000u 0:00.000

Here is a diff of 'identify -verbose':
http://sprunge.us/NWJK


Here are the files:
https://dev.zco.mx/files/convert-gif.zip

I tested with the simple convert command I was using last week as well
as the more complex command Nicolas shared and get similar results.

User avatar
fmw42
Posts: 25556
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert - downscaling comic pages

Post by fmw42 » 2014-08-28T16:51:15-07:00

The resampling is creating new colors so that the output gif may have more colors than you started. The best suggestion might be to get the unique-colors from the input images before processing. Convert those colors into 1px images and append together into a 1D remap image. Then use -remap at the end to map your output colors back to the same input colors. See

http://www.imagemagick.org/Usage/quantize/#extract
http://www.imagemagick.org/script/comma ... que-colors
http://www.imagemagick.org/Usage/quantize/#remap


If you are on unix, then the following will create the 1D remap image.

If your image is not gif or PNG8, then use the following and you can change/reduce the -colors from 256 to something closer to what you have in the input image.

Code: Select all

colors=`convert yourimage +dither -colors 256 -unique-colors txt: | tail -n +2 | sed -n 's/^.*[\#][0-9a-fA-F]* [ ]*\(.*\)$/xc\:"\1"/p' | sed 's/\s//g'`
echo $colors
eval convert -size 1x1 $colors +append colormap.png
If you image is gif or PNG8, then you can leave off -colors, since the input gif or png8 will have no more than 256 colors and in your case likely much fewer:

Code: Select all

colors=`convert yourimage -unique-colors txt: | tail -n +2 | sed -n 's/^.*[\#][0-9a-fA-F]* [ ]*\(.*\)$/xc\:"\1"/p' | sed 's/\s//g'`
echo $colors
eval convert -size 1x1 $colors +append colormap.png
Sorry I do not know how to do this in Windows.

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 » 2014-08-29T00:25:59-07:00

steve___ wrote:Is there a way to resize gif's so the file sizes does not become large?
Another way is to median filter the input image with a support that roughly matches the square of the downsampling ratio (so if you are downsampling to about 1/3 of the width and height, median filter with a 3x3) then use nearest neighbour (-filter Point) to downsample, then making sure that having converted to floats does not mess things up. Using -depth 8 at the end should do the trick (may not be necessary with gifs).
You could also use -filter Point without any median filtering, but results are probably not going to always look good.

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 » 2014-08-29T03:45:43-07:00

Thank you Steve for being so persistent.
This is very informative.

steve___
Posts: 28
Joined: 2012-07-17T09:52:07-07:00
Authentication code: 15

Re: convert - downscaling comic pages

Post by steve___ » 2014-08-30T11:37:37-07:00

Ha, thanks to you guys. If it wasn't for the help in this thread I'd
still be floundering in my own ignorance.

Here is the main part of my script:

Code: Select all

_colourmap() {
    unset cj cm nc
    if [[ ${f##*.} == jpg ]]; then
        nc=$(identify -format '%k' "$f"[0])
        (( $nc > 256 )) && return
        cj="+dither -colors $nc"
        nf=${nf/.jpg/.png}
    fi

    colours=$(convert "$f" $cj -unique-colors txt: | awk -v _=\" 'NR>1 {print "xc:"_$4_}')
    eval convert -size 1x1 $colours +append colourmap.png
    cm="-remap colourmap.png"
}

filter=RobidouxSharp
_colourmap
convert \( "$f[0]" -quiet -define jpeg:preserve-settings -colorspace $cs \) \
    \( -clone 0 -gamma 1.666666666666666 -filter $filter -distort Resize $res -gamma 0.6 \) \
    \( -clone 0 -filter $filter -distort Resize $res \) -delete 0 \
    \( -clone 1 -colorspace gray -auto-level \) -compose over -composite \
    -set colorspace $cs -colorspace sRGB -depth 8 $cm +repage "$nf"
rm colourmap.png &>/dev/null

thehau
Posts: 1
Joined: 2014-08-31T19:14:19-07:00
Authentication code: 6789
Location: Florida

Re: convert - downscaling comic pages

Post by thehau » 2014-08-31T19:23:15-07:00

fmw42 wrote:The resampling is creating new colors so that the output gif may have more colors than you started. The best suggestion might be to get the unique-colors from the input images before processing. Convert those colors into 1px images and append together into a 1D remap image. Then use -remap at the end to map your output colors back to the same input colors. See

http://www.imagemagick.org/Usage/quantize/#extract
http://www.imagemagick.org/script/comma ... que-colors
http://www.imagemagick.org/Usage/quantize/#remap


If you are on unix, then the following will create the 1D remap image.

If your image is not gif or PNG8, then use the following and you can change/reduce the -colors from 256 to something closer to what you have in the input image.

Code: Select all

colors=`convert yourimage +dither -colors 256 -unique-colors txt: | tail -n +2 | sed -n 's/^.*[\#][0-9a-fA-F]* [ ]*\(.*\)$/xc\:"\1"/p' | sed 's/\s//g'`
echo $colors
eval convert -size 1x1 $colors +append colormap.png
If you image is gif or PNG8, then you can leave off -colors, since the input gif or png8 will have no more than 256 colors and in your case likely much fewer:

Code: Select all

colors=`convert yourimage -unique-colors txt: | tail -n +2 | sed -n 's/^.*[\#][0-9a-fA-F]* [ ]*\(.*\)$/xc\:"\1"/p' | sed 's/\s//g'`
echo $colors
eval convert -size 1x1 $colors +append colormap.png
Hah, Thanks fmw42! What you said is exactly what I am looking

Post Reply