Re: quality of downsampled images once JPEG compressed
Posted: 2011-04-16T18:47:14-07:00
				
				I should have posted the code outside of the tar archive. Here it is. It is based on code Fred put together earlier.fmw42 wrote:Can you explain what you mean by this? Sounds like minifying by enlarging, but that seems rather strange.Doing it "straight" ("no blur" postfix) and doing it by enlarging -define filter:blur depending on the requested quality ("blur" postfix)
Code: Select all
#!/bin/bash
# For best results, use with IM compiled with --enable-hdri.
# Reason:
# The conversion to YCbCr may lead to negative (intermediate) color
# values. I (Nicolas Robidoux) am not completely sure that conversion
# to YCbCr for the resampling part of the computation is totally
# necessary.
# Rationale for the conversion:
# 1) Conversion to a "linear" colorspace for resampling purposes, and
# 2) (hopefully) smoother values in the colorspace actually used by
# JPEG, which leads to smaller compressed files for a fixed quality
# level.
# test image from http://www.pbase.com/konascott/image/69543104/original
infile="original.jpg"
# Warning: quality = 0 has a special meaning.
# A wide range of quality value gives good results. Without actively
# looking for artifacts, knowing what to look for, or zooming in,
# no-one should notice any compression artifacts above, more or less,
# quality = 80. For thumbnails produced with the lanczos2sharp filter
# (see below), compression artifacts are nearly invisible, even when
# zooming, at quality = 98.
# Note that "2x2" below hardwires chroma subsampling (meaning that
# 4:2:0 chroma subsampling is always on, irregardless of the requested
# quality).  IMHO, 4:2:0 should be used even when the quality is high
# and the thumbnail small. If you want "true" maximal quality, remove
# "-sampling-factor 2x2" when you are using a very high quality level
# (97 and above, say), even though it is a waste from a perceptual
# viewpoint.
size="512x512"
# I don't believe that -distort resize works in PerlMagick, and I
# don't think that the lanczos2sharp and lanczossharp filters are
# available ether (they should only be used with distort anyway).
# Use the slightly inferior (IMHO)
#   -filter lanczos -resize
# or
#   -filter lanczos2 -resize
# instead.
# I don't think that additional sharpening is needed with distort
# lanczossharp, distort lanczos2sharp, or resize lanczos. Maybe (?)
# with resize lanczos2. For a fixed quality level, sharpening
# Increases file size and the visibility of JPEG artifacts. Let me
# know if you want additional sharpening.
# In the following, I set the extra "blur" for the versions with extra
# smoothing by interpolating linearly from 1.5 when quality = 0 to 1
# (=none) when quality = 100. I've not studied things too carefully,
# but this seems to strike a reasonable balance between JPEG artifact
# reduction and sharpness.
for quality in 30 40 50 60 65 66 67 68 69 70 75 80 85 90 95 98; do
    for filter in lanczos2sharp lanczos lanczossharp; do
	convert $infile \
	    -colorspace YCbCr \
	    -filter $filter \
 	    -distort resize $size^ \
	    -gravity Center \
	    -crop $size+0+0 \
	    +repage \
	    -colorspace sRGB \
	    -sampling-factor 2x2 \
	    -quality $quality \
	    -strip \
 	    $quality\_$filter\_noblur.jpg
	blur=$(echo "1.5-.005*$quality" | bc -q 2>/dev/null)
	convert $infile \
	    -colorspace YCbCr \
	    -filter $filter \
	    -define filter:blur=$blur \
 	    -distort resize $size^ \
	    -gravity Center \
	    -crop $size+0+0 \
	    +repage \
	    -colorspace sRGB \
	    -sampling-factor 2x2 \
	    -quality $quality \
	    -strip \
 	    $quality\_$filter\_blur.jpg
    done
done
for quality in 30 40 50 60 65 66 67 68 69 70 75 80 85 90 95 98; do
    for filter in lanczos; do
	convert $infile \
	    -colorspace YCbCr \
	    -filter $filter \
	    -resize $size^ \
	    -gravity Center \
	    -crop $size+0+0 \
	    +repage \
	    -colorspace sRGB \
	    -sampling-factor 2x2 \
	    -quality $quality \
	    -strip \
	    $quality\_resize\_noblur.jpg
	blur=$(echo "1.5-.005*$quality" | bc -q 2>/dev/null)
	convert $infile \
	    -colorspace YCbCr \
	    -filter $filter \
	    -define filter:blur=$blur \
	    -resize $size^ \
	    -gravity Center \
	    -crop $size+0+0 \
	    +repage \
	    -colorspace sRGB \
	    -sampling-factor 2x2 \
	    -quality $quality \
	    -strip \
	    $quality\_resize\_blur.jpg
    done
done
-------
RE: HDRI:
I'm not totally sure its needed, but I'm converting to YCbCr which, if I understand properly, can get negative values if converted to/from, say, RGB or sRGB. So, I've not checked that the script gives bad results if you don't use and hdri-enabled IM, I just played it safe, given that my understanding is that non-hdri IM won't store negative intermediate values.

