Provoked by @JMichaelTX's question "What is ImageMagick", here an AppleScript that allows you to tweak your screenshots in several ways.
The script has a few dependencies:
- ImageMagick
- advpng (aka advancecomp)
- pngquant
How to install these programs via Homebrew is explained in the header of the script.
You can run the script either as it is (e.g. from the Script menu or via LaunchBar etc.), or as a KM Macro.
Just select one or more screenshots (png files) in the Finder and run the script.
The standalone version:
IM-Screenshot-Prettifier.scpt.zip (29.8 KB)
The KM Macro:
Screenshot Prettifier.kmmacros (12.3 KB)
The script text:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"net.dflect.IMScreenshotPrettifier"
theDefaults's registerDefaults:{binPath:"export PATH=/usr/local/bin;", options:"", shadowValue:"80x3+2+2", polaroidValue:"-.5", usmValueForResized:".8x.8+1+0.55", usmValueForUnchangedSize:".8x.8+1+0.5", usmValueForPolaroid:".8x.8+1+0.35", polaroidCaption:"-gravity center -font SF-UI-TextI -pointsize 20 -fill maroon -set caption 'Look at this nice image!'", option1:"", option2:"", nonPNGs:0, helpMsg:"You can choose one item from each group.
Polaroid applies USM by default, so do not combine it with USM.
Choose Polaroid… to set the angle. If you press Save & OK a Polaroid action will be executed with that angle. If you press Save & Relaunch you can run the Polaroid w/Caption action with that angle value."}
# Good values for Unsharp Mask: [radius x sigma + amount + threshold]
# usmValueForResized:".8x.8+1+0.55"
# usmValueForUnchangedSize:".8x.8+1+0.5"
# usmValueForPolaroid:".8x.8+1+0.35"
set binPath to binPath of theDefaults as text
set options to options of theDefaults as list
set shadowValue to shadowValue of theDefaults as text
set polaroidValue to polaroidValue of theDefaults as text
set usmValueForResized to usmValueForResized of theDefaults as text
set usmValueForUnchangedSize to usmValueForUnchangedSize of theDefaults as text
# I have added an “unsharp” to the Polaroid action by default because it seems to use “resize” to shrink the image (“resize” is more blurry than “scale”)
set usmValueForPolaroid to usmValueForPolaroid of theDefaults as text
set polaroidCaption to polaroidCaption of theDefaults as text
set option1 to option1 of theDefaults as text
set option2 to option2 of theDefaults as text
set nonPNGs to nonPNGs of theDefaults as integer
set helpMsg to helpMsg of theDefaults as text
global binPath, outNamePrettyQuant, outNamePretty, outNameQuant
# Collect files
tell application "Finder"
set trgImages to selection as alias list
repeat while (count of trgImages) < 1
display alert "Please select one or more image files (PNG) in the Finder." as warning buttons {"Cancel", "Continue"}
if the result's button returned is "Cancel" then error number -128
set trgImages to selection as alias list
end repeat
end tell
# Get options. We can choose one of the first group + Quantize.
set prevOptions to options
tell application (path to frontmost application as text)
set options to choose from list {"Drop Shadow…", "Drop Shadow", "Polaroid…", "Polaroid", "Polaroid w/Caption…", "Torn Edges", "Torn Edges +Shadow", "Thick", "--------------", "Quantize", "--------------", "Resize to 50 %", "Resize to 70 %", "Resize to 75 %", "Resize to 80 %", "Resize to 85 %", "Resize to 90 %", "Shrink to 300 px Width", "Shrink to 350 px Width", "Shrink to 400 px Width", "USM for Original Size", "--------------", "Help"} with title "Options" with prompt "Hold down ⌘ to select one option of each group." OK button name "OK" cancel button name "Cancel" default items options multiple selections allowed yes empty selection allowed no
if options is false then error number -128
end tell
# Customization for some options
if options contains "Drop Shadow…" then
set shadowValue to the text returned of (display dialog "Set shadow parameters (e.g. 80x3+2+2):" default answer shadowValue)
set shadowValue of theDefaults to shadowValue
set item 1 of options to "Drop Shadow"
else if options contains "Polaroid…" then
set theResult to (display dialog "Set polaroid angle (e.g. .9):" default answer polaroidValue buttons {"Cancel", "Save & Relaunch", "Save & OK"} default button "Save & OK")
set polaroidValue to theResult's text returned
set polaroidValue of theDefaults to polaroidValue
set item 1 of options to "Polaroid"
if the theResult's button returned is "Save & Relaunch" then
set item 1 of options to "Polaroid w/Caption…"
run me
end if
end if
# Set options
if options contains "Drop Shadow" then
# http://www.imagemagick.org/Usage/blur/#shadow
set option1 to "\\( +clone -background black -shadow" & space & shadowValue & space & "\\) +swap -background none -layers merge +repage"
else if options contains "Polaroid" then
# http://www.imagemagick.org/Usage/transform/#polaroid
# Nice colors: *OldLace, linen, *WhiteSmoke, moccasin, AntiqueWhite, PapayaWhip
set option1 to "-bordercolor WhiteSmoke -background gray30 -polaroid" & space & polaroidValue & space & "-unsharp" & space & usmValueForPolaroid
else if options contains "Polaroid w/Caption…" then
set polaroidCaption to the text returned of (display dialog "Set caption:" default answer polaroidCaption)
set polaroidCaption of theDefaults to polaroidCaption
set option1 to "-bordercolor GhostWhite -background gray30" & space & polaroidCaption & space & "-polaroid" & space & polaroidValue & space & "-unsharp" & space & usmValueForPolaroid
else if options contains "Torn Edges" then
# http://www.imagemagick.org/Usage/thumbnails/#torn
set option1 to "\\( +clone -alpha extract -virtual-pixel black -spread 10 -blur 0x3 -threshold 50% -spread 1 -blur 0x.7 \\) -alpha off -compose Copy_Opacity -composite"
else if options contains "Torn Edges +Shadow" then
set option1 to "\\( +clone -alpha extract -virtual-pixel black -spread 10 -blur 0x3 -threshold 50% -spread 1 -blur 0x.7 \\) -alpha off -compose Copy_Opacity -composite - | convert - \\( +clone -background black -shadow 50x2+2+2 \\) +swap -background none -layers merge +repage"
else if options contains "Thick" then
# http://www.imagemagick.org/Usage/thumbnails/#thickness
set option1 to "-alpha set \\( +clone -fill LightSteelBlue3 -colorize 100% -repage +0+1 \\) \\( +clone -repage +1+2 \\) \\( +clone -repage +1+3 \\) \\( +clone -repage +2+4 \\) \\( +clone -repage +2+5 \\) \\( +clone -repage +3+6 \\) \\( +clone -repage +3+7 \\) \\( +clone -repage +4+8 \\) \\( +clone -repage +4+9 \\) -background none -compose DstOver -mosaic"
end if
# These options are combinable with the first option (option1); should always be the last item in the list
if item -1 of options is "Help" then
display alert "Hints:" message helpMsg
set options to prevOptions
run me
error number -128
else
tell item -1 of options
if it contains "%" then
set scaleValue to word -1
set option2 to "-scale" & space & scaleValue & "%" & space & "-unsharp" & space & usmValueForResized
else if it contains "width" then
set scaleValue to word -3
set option2 to "-scale" & space & scaleValue & "\\>" & space & "-unsharp" & space & usmValueForResized
else if it contains "USM" then
set option2 to "-unsharp" & space & usmValueForUnchangedSize
end if
end tell
end if
set options of theDefaults to options
# Main part
repeat with image in trgImages
set image to POSIX path of image
# Get path components
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set extension to text item -1 of image
set head to (text items 1 through -2 of image) as text
set AppleScript's text item delimiters to saveTID
# Most of the stuff runs only on pngs
if extension is "png" then
if item 1 of options is "Quantize" and (count of options) is 1 then
# For convenience, quantization only
quantize(image, extension, head)
# Advpng does never harm. If too slow set it to -z3 or -z2.
advpng(outNameQuant)
else if options contains "Quantize" then
# One IM command + quantization (as stdin/stdout combo)
combo(image, extension, head, option1, option2)
advpng(outNamePrettyQuant)
else
# Only IM, without quantization
prettify(image, extension, head, option1, option2)
advpng(outNamePretty)
end if
else
set nonPNGs to nonPNGs + 1
end if
end repeat
display notification (((count of trgImages) - nonPNGs) as text) & " image(s) processed." with title "Screenshot Prettifier" sound name "Submarine"
if nonPNGs > 0 then
display alert (nonPNGs as text) & " non-PNG files found. Only PNG files have been processed!" as warning buttons "OK"
end if
### Handlers #######################################################
# A pure IM command
on prettify(image, extension, head, option1, option2)
set outNamePretty to head & "-pty." & extension
do shell script binPath & space & "convert" & space & quoted form of image & space & ¬
option1 & space & option2 & space & quoted form of outNamePretty
end prettify
# Pure Pngquant
on quantize(image, extension, head)
set outNameQuant to head & "-fs8." & extension
do shell script binPath & space & "pngquant --quality=70-95 --floyd --skip-if-larger --force --speed=1 --output" & ¬
space & quoted form of outNameQuant & space & quoted form of image
end quantize
# Both combined (preferable, since no intermediate file writing)
on combo(image, extension, head, option1, option2)
set outNamePrettyQuant to head & "-pty-fs8." & extension
do shell script binPath & space & "convert" & space & quoted form of image & space & ¬
option1 & space & option2 & space & "- | pngquant - --quality=70-95 --floyd --force --speed=1 --output" & ¬
space & quoted form of outNamePrettyQuant
end combo
# Lossless advpng (doesn't read from stdin). -z3 (7zip lib) is pretty good _and_ fast.
on advpng(input)
do shell script binPath & space & "advpng -z3" & space & quoted form of input
end advpng
# Not significantly better than advpng -z3
--on zopfli(input)
-- do shell script binPath & space & "zopflipng --lossy_transparent -y" & ¬
-- space & quoted form of input & space & quoted form of input
--end zopfli
Examples
The script allows you to quickly produce screenshots like this:
…with a decent shadow:
…with torn edges and a slight shadow:
… as polaroid with caption:
…and some more.
Feel free to modify the ImageMagick parameters in the script. They are far from being optimal and you have a lot of choices
Comments in the script are pointing you to sources of information.
Hint:
If you have a "Retina" screen and your image is more than 690px wide choose "Resize to 50%".