ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: Andreas Spindler on December 29, 2014, 05:22:20 AM

Title: Auto-sharpen images with low contrast/sharpness
Post by: Andreas Spindler on December 29, 2014, 05:22:20 AM
Background: As many photographers I had shoot with low contrast/sharpness in-camera settings, because it was told to us everywhere that PC programs are so much better. Olympus cameras allow settings from -5 to +5, for example, and although Olympus ever was praised for its good JPEG engines, I'd shot with -1 and -2. The idea was to sharpen them on the PC later. But this never happened.

Motivation: Write a script that goes through a folder and resharpen all JPEG files with low contrast/sharpness using ImageMagick tools.

Idea: Do it all in one ExifTool arguments file.

Start:
exiftool -@ test.args INPUT-DIRECTORY

-r
-ext
jpg
-ext
jpeg
-if
($ContrastSetting eq "-2 (min -5, max 5)"  or $ContrastSetting eq "-1 (min -5, max 5)"  or $ContrastSetting eq "0 (min -5, max 5)")
-if
($SharpnessSetting eq "-2 (min -5, max 5)" or $SharpnessSetting eq "-1 (min -5, max 5)" or $SharpnessSetting eq "0 (min -5, max 5)")
-FileName
-ContrastSetting
-SharpnessSetting


This identifies all JPEG files with low contrast/sharpness settings and then prints out filenames. ContrastSetting and SharpnessSetting are values for Olympus maker notes.

Problem 1: Identify only such JPEGs with a RAW companion.

Problem 2: It would be nice to use -n to be more precise, like -if $ContrastSetting < 0. But -ContrastSetting prints three values (actual, min and max). How to access the first?

Problem 3: How to run an external program on $Filename.

Problems #1 and #2 are actually minor. There are many examples about -n but they all borrow from Phil's original -Orientation example which is only one value.

Regarding #3, how to execute something on $Filename remained obscure. It is clear that a Bash wrapper wouldn't be difficult, but Phil's latest "A Note to Unix Power-Users" made me think that executing a command dependend on -if's could be possible. :) Is it?

Thanks!
Title: Re: Auto-sharpen images with low contrast/sharpness
Post by: Phil Harvey on December 29, 2014, 09:00:21 AM
Hi Andreas,

Quote from: Andreas S. on December 29, 2014, 05:22:20 AM
Problem 1: Identify only such JPEGs with a RAW companion.

You would have to do this with your own script.  The ExifTool -if option only works on tags from the target file.

QuoteProblem 2: It would be nice to use -n to be more precise, like -if $ContrastSetting < 0. But -ContrastSetting prints three values (actual, min and max). How to access the first?

You could do this using an advanced formatting expression:  ${contrastsetting;s/ .*//} < 0

QuoteProblem 3: How to run an external program on $Filename.

ExifTool won't run an external program, so in this case my note to Unix power-users doesn't apply.

- Phil
Title: Re: Auto-sharpen images with low contrast/sharpness
Post by: Hayo Baan on December 29, 2014, 02:02:10 PM
Quote from: Phil Harvey on December 29, 2014, 09:00:21 AM
QuoteProblem 3: How to run an external program on $Filename.

ExifTool won't run an external program, so in this case my note to Unix power-users doesn't apply.

You should actually be able to use the perl system or backtick commands to run an external program ;)
Title: Re: Auto-sharpen images with low contrast/sharpness
Post by: Phil Harvey on December 29, 2014, 02:59:36 PM
Hi Hayo,

True, but not a designed feature, and not recommended.  One would have to embed the system command inside a -if argument or something similarly sneaky.

- Phil
Title: Re: Auto-sharpen images with low contrast/sharpness
Post by: Andreas Spindler on December 30, 2014, 12:15:30 AM
Quote from: Phil Harvey on December 29, 2014, 02:59:36 PM
...to embed the system command inside a -if argument or something similarly sneaky.

Thought of this too - embedding a system() call in -if. After sleeping a night over it I'm not sure if it is really sneaky. -if's feel like the standard find expressions, because they are also and-ed together. Since -if tunnels any Perl expression, and in the absence of -exec, people will do it anyway sooner or later. Argument files are the shortest path to program Exiftool, and finally you can't beat things that work.

-if
system('exiftool -ver')


> exiftool -@ exec.args dummy.jpg
9.72
    1 files failed condition


-if
system('exiftool -ver') or 1
-Filename


> exiftool -@ exec.args dummy.jpg
9.72
File Name: dummy.jpg


Aha. However, I finished a shell script that does the job. To whom it may concern...

#!/bin/bash
#
# autoconvert -- Resharpen Jpegs with low contrast settings
#

if [[ $(uname -s) =~ ^CYGWIN* ]]; then
find=/usr/bin/find; exiftool=/usr/local/bin/exiftool
else
find=find; exiftool=exiftool
fi

optdir=$1
#optmethod=print
optmethod=sharpen

cat <<'HERE' >criterium.args
-v0
-m
-q
-q
-if
${OLYMPUS:ContrastSetting;s/ .*//}  <= 0
-if
${OLYMPUS:SharpnessSetting;s/ .*//} <= 0
-Filename
-OLYMPUS:ContrastSetting
-OLYMPUS:SharpnessSetting
HERE

$find "$optdir" -type f -iname '*.jp*g' -print0 |
while read -r -d $'\0' file
do
if $exiftool -@ criterium.args "$file"; then
case $optmethod in
sharpen)
cp -vu $file $file.del
convert $file -unsharp 0×3+1.5+0.0196 -quality 92 $file
$exiftool \
-overwrite_original_in_place -P \
-OLYMPUS:ContrastSetting='1 -5 5' \
-OLYMPUS:SharpnessSetting='1 -5 5' "$file"
;&
print)
$exiftool -Filename -OLYMPUS:ContrastSetting -OLYMPUS:SharpnessSetting "$file"
;;
esac
fi
done
rm criterium.args


Still fighting with the -unsharp option because it should actually depend on the SharpnessSetting found in the input file. These are good readings:

http://www.imagemagick.org/Usage/blur/ (http://www.imagemagick.org/Usage/blur/)

http://www.fmwconcepts.com/imagemagick/sharp/index.php (http://www.fmwconcepts.com/imagemagick/sharp/index.php)

http://redskiesatnight.com/2005/04/06/sharpening-using-image-magick/ (http://redskiesatnight.com/2005/04/06/sharpening-using-image-magick/)

Without -quality ImageMagick converted 12MP JPEGs from ca. 5,5 MB to 8 MB. convert tries to estimate the JPEG quality, and it did it bad, since all JPEGs have been shot already in the highest quality mode "Super Large Fine". This quality mode is, as I learned after using Olympus cameras for years now, definitely too high if you shoot RAW+JPEG. The explicit setting -quality 92 (ImageMagick uses 92 as a default) then shocked by reducing the file size to 2.5 MB. But that seems to be ok:

http://regex.info/blog/lightroom-goodies/jpeg-quality (http://regex.info/blog/lightroom-goodies/jpeg-quality)
Title: Re: Auto-sharpen images with low contrast/sharpness
Post by: Andreas Spindler on December 30, 2014, 01:39:27 AM
Here are some sample outputs from the above script. The original file had been made over 4 years ago with an Olympus E-30 and never touched since. You may download the original file to simulate the results. Sorry, the image is a bit dark and noisy - it was just the first in my Lab folder with images that need attention.

http://www.visualco.de/temp/forenbilder/exiftool/P5028968.jpg (http://www.visualco.de/temp/forenbilder/exiftool/P5028968.jpg)
Original file Super-Large-Fine Olympus E-30 JPEG, ~5.35MB, taken in the Parc Natural de l'Albufera, Mallorca (http://www.albufera.com/parque/ (http://www.albufera.com/parque/), http://de.wikipedia.org/wiki/Parc_natural_de_s%E2%80%99Albufera_de_Mallorca).

http://www.visualco.de/temp/forenbilder/exiftool/P5028968-quality97.jpg (http://www.visualco.de/temp/forenbilder/exiftool/P5028968-quality97.jpg)
Converted with -unsharp 0×3+1.5+0.0196 -quality 99, ~6.37MB.

http://www.visualco.de/temp/forenbilder/exiftool/P5028968-quality69.jpg (http://www.visualco.de/temp/forenbilder/exiftool/P5028968-quality69.jpg)
Converted with -unsharp 0×3+1.5+0.0196 -quality 69, ~862K.

$ convert -version
Version: ImageMagick 6.9.0-0 Q16 i686 2014-12-21 http://www.imagemagick.org


Note that -unsharp 0×3+1.5+0.0196 was mentioned on the ImageMagick documentation (see the link posted before).