Skip to content

Instantly share code, notes, and snippets.

@majioa
Created May 8, 2013 07:07
Show Gist options
  • Save majioa/5538760 to your computer and use it in GitHub Desktop.
Save majioa/5538760 to your computer and use it in GitHub Desktop.
Smart Sharpening of an image with ImageMagick app
#!/bin/bash
sharp_radius=5
sharp_sigma=$(dc <<< 4k${sharp_radius}vp)
sharp_amount=1.2
sharp_threshold=0.003
a1=0
b1=0
c1=1.2
d1=-0.1
blur_radius=3
blur_sigma=$(dc <<< 4k${blur_radius}vp)
edge_width=2
clip_low="40" #percentage left totally untouched by the sharpening
clip_high="1" #percentage sharpened full strength
prefix="intermediates"
outdir="finals"
image=$(sed "s/.jpg//" <<< $1)
echo $sharp_sigma $blur_sigma $image
mkdir -p $prefix
mkdir -p $outdir
#first make the edges
convert $image.jpg -edge $edge_width -colorspace Gray ${prefix}/${image}_inner_edges.jpg
convert $image.jpg -negate -edge $edge_width -colorspace Gray ${prefix}/${image}_outer_edges.jpg
composite -compose plus ${prefix}/${image}_inner_edges.jpg ${prefix}/${image}_outer_edges.jpg ${prefix}/${image}_added_edges.jpg
convert ${prefix}/${image}_added_edges.jpg -gaussian-blur ${blur_radius}x${blur_sigma} ${prefix}/${image}_edges_blurred.jpg
convert ${prefix}/${image}_edges_blurred.jpg -linear-stretch ${clip_low}\%x${clip_high}% ${prefix}/${image}_edges.jpg
#cp ${image}_edges_blurred.jpg ${image}_edges.jpg
#convert ${image}_edges_blurred.jpg -linear-stretch ${clip_low}\%x${clip_high}% ${image}_edges.jpg
#convert ${image}_edges_blurred.jpg -function Polynomial $a1,$b1,$c1,$d1 ${image}_edges.jpg
#then get the value channel and sharpen it
convert $image.jpg -colorspace HSB -channel B -separate ${prefix}/${image}_value.jpg
convert ${prefix}/${image}_value.jpg -unsharp ${sharp_radius}x${sharp_sigma}+${sharp_amount}+${sharp_threshold} ${prefix}/${image}_value_8.jpg
composite ${prefix}/${image}_value_8.jpg ${prefix}/${image}_value.jpg ${prefix}/${image}_edges.jpg ${prefix}/${image}_value_partial_sharp.jpg
#then get the hue and saturation channels
convert $image.jpg -colorspace HSB -channel R -separate ${prefix}/${image}_hue.jpg
convert $image.jpg -colorspace HSB -channel G -separate ${prefix}/${image}_saturation.jpg
#finally put it all together
convert ${prefix}/${image}_hue.jpg -colorspace HSB ${prefix}/${image}_hue.jpg -compose CopyRed -composite ${prefix}/${image}_saturation.jpg -compose CopyGreen -composite ${prefix}/${image}_value_partial_sharp.jpg -compose CopyBlue -composite -colorspace RGB ${outdir}/${image}_smartsharp.jpg
#clean up workspace
/bin/rm ${prefix}/${image}_hue.jpg ${prefix}/${image}_saturation.jpg
#system("/bin/rm ${image}_inner_edges.jpg ${image}_outer_edges.jpg ${image}_edges_blurred.jpg ${image}_added_edges.jpg ${image}_edges.jpg ${image}_value.jpg ${image}_value_8.jpg ${image}_value_partial_sharp.jpg");
cp -t finals ${image}.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment