TinyPNG as one of the steps?

Comparing index PNG to tinyPNG always results in smaller but better files coming out of tinyPNG. Something about their dithering perhaps?

For instance compare best quality 256 indexed png:
image

To drag into TinyPNG
image

TinyPNG is also about 200k smaller on this particular file.

So, I’m wondering if it can run the file through TinyPNG via AppleScript or Shell?

I’ve found the following for automator shell set to bin/bash as arguments. But I’m not sure how to implement it into Retrobatch? I don’t need it to ask my api each time. I’d rather just enter it into the script once and forget it.

CONFIG=~/Library/Caches/TinyPNG
if [ -f $CONFIG ]; then
APIKEY=$(<$CONFIG)
else
APIKEY="$(osascript -e ‘Tell application “System Events” to display dialog “Enter a TinyPNG api key:” default answer “”’ -e ‘text returned of result’ 2>/dev/null)"
if [ $? -ne 0 ]; then
# The user pressed Cancel
exit 1 # exit with an error status
elif [ -z “$APIKEY” ]; then
# The user left the project name blank
osascript -e ‘Tell application “System Events” to display alert “You must enter a TinyPNG api key; cancelling…” as warning’
exit 1 # exit with an error status
fi
echo “$APIKEY” > $CONFIG
fi

for f in “$@”
do
echo $f | while IFS= read file
do
filename=$(basename “$file”)
ext=$(echo ${filename##*.} | tr “[:upper:]” “[:lower:]”)
if [ -f “$file” ]
then
if ( [ $ext == “png” ] || [ $ext == “jpg” ] || [ $ext == “jpeg” ] )
then
JSON=curl -i --user api:"$APIKEY" --data-binary @"$file" https://api.tinypng.com/shrink 2>/dev/null
if [[ $JSON == error ]]
then
ERROR=${JSON/error":"/}
ERROR=${ERROR/","
/}
osascript -e ‘Tell application “System Events” to display alert “The TinyPNG api key is wrong!; cancelling…” as warning’
exit 1
else
URL=${JSON/url":"/}
URL=${URL/"
/}
curl $URL>"${file}" 2>/dev/null
fi
fi
fi
done

done

Any ideas? Thanks!

This is where it looks for the API KEY.

Keeping that outside of the script is more secure and also would let you share the script without having to remember to remove your key (as did whoever shared it online)

Thanks Gingerbeardman! I’ll put it there. Do you know how to get it working within Retrobatch as a node?

You’ll want to take you script and and save it as a .sh file, and then use the Shell Script node to point to it. When each image is run through the script, the first argument to the script is the location of the image. You might need to tweak your script a little for it, but it should be doable.

1 Like