Friday, October 07, 2005

img2img

So I was trying to think up a way of converting my Warcraft screenshots from 5MB TARGAs to 1MB PNGs or 500K JPEGs, but the OS X Preview's Automator actions don't convert them very well. My instinct was then to turn to powerful UNIX tools. I installed ImageMagick, and wrote up a quick bash script you see below. You can change OLDSUFFIX and NEWSUFFIX to whatever you want, and you've got a quick-hack anyimg2anyimg script.
#!/bin/bash

OLDSUFFIX=tga
NEWSUFFIX=jpg
CONVERT=`which convert`
FILEPATH=$1

if [ -z "$CONVERT" ]; then
echo "error: cannot find \'convert\'";
exit 1;
fi
if [ -z "$FILEPATH" ]; then
echo "error: no filepath provided";
exit 1;
fi

DIR=`dirname $FILEPATH`
FILE=`basename $FILEPATH`
NAME=`echo $FILE | cut -d"." -f1`

cd $DIR
$CONVERT $NAME.$OLDSUFFIX $NAME.$NEWSUFFIX
cd -


So you can make calls to this in an Automator workflow, and make it a right-click script. I'll be damned if I can get a stupid workflow to work right though. I've uploaded what workflow I have in case you guys want to make one work.

4 Comments:

At 3:23 PM, Anonymous Anonymous said...

It's the weirdest thing. For some reason if you stick an action between the "get items" and "shell script" actions, it works. Otherwise it doesn't. (by "works" i just mean overall, i didn't really test your tga2png or anything - just using sample echo $@ type of scripts).

Try it. Put a "Ask for confirmation" dialog in there.

I have no idea why this is.. but I'll probably look into it more later.

Also btw, you should probably quote the filenames and such:

DIR=`dirname "$FILEPATH"`
FILE=`basename "$FILEPATH"`
NAME=`echo "$FILE" | cut -d"." -f1`
cd "$DIR"
$CONVERT "$NAME.$OLDSUFFIX" "$NAME.$NEWSUFFIX"
cd -

 
At 11:09 PM, Anonymous Anonymous said...

Nope. I added a Ask for Confirmation action, and it still doesn't work. I suspect its the way the Run Shell Script action is behaving and executing my tga2png script.

 
At 1:01 PM, Anonymous Anonymous said...

to get it to work I made some other changes (which I figured were specific to your system and testing).... Like you had "specified" finder items, i changed that to "selected". You also had $HOME/.bin/stuff which i changed to $HOME/bin/stuff. And some other minor things, like quoting all the filenames.

To test it, you can change your code to do echo "$filename" instead of running the script, then sticking a "view results" action from the Automator action list to view its output.

 
At 8:48 PM, Anonymous Anonymous said...

Yea, I have ~/.bin rather than ~/bin. No reason not too, and it keeps my home in Finder looking less "Unix". I'll keep banging at it, though at this point, its faster to just open up a damn terminal and run it the old fashioned way.

 

Post a Comment

<< Home