Friday, September 17, 2010

Batch Transform (transparent) Images

I used the following to remove the backgrounds from around 32,000 sprites in about 4 hours. My sprites are also now all in English with no spaces (only way I could get the Automator shell script to work).

Requires ImageMagick, an open source command line image processing utility. The easiest way to install it is by using MacPorts.

Phase One (prep all folders)
Pre-process Automator workflow to rename all sub folders from " " to "-" (eg "skel with sword" to "skel-with-sword"). This is required for the entire path all the way to (and including) the .png file, so to make it easy I moved my artwork folder to my desktop. This same workflow is also used to rename all the image files. Now that all the spaces have been removed the shell script inside the final Automator workflow will run without errors (yeah spent about 4 hours on that problem and if someone can get it to work with spaces in the path / file name, please let me know).

Phase Two (prep all sprites)
Using another Automator workflow, renamed all sprites to use English (e.g. Lauft = Running) words. I also used this script to update the base names (e.g. SkelSword, SkelStaff, SkelBow, etc). Note: this is not required at all but I figured while I was at it, might as well make the files easier for me to use.

Phase Three (individual sprite folders)
1: Copy one of the sprite images to the desktop, rename it "background.png" and then open in Pixen and make it a solid color using the existing background. This file is then used by the shell script with ImageMagick to remove itself, i.e. make transparent, against the other image(s).
2: To process we run a third Automator workflow and select all the images to be made transparent (that match background.png). Repeat steps 1 & 2 above for each folder of sprites.
Note: if you skip step 1 because the background looks really close to the previous one, you may end up with 3/4 sprites (I know I did!) and it wasn't worth the few seconds it takes to create the new background.png file.
Speed: On my 2.4GHz iMac it would take less than 2 minutes to process ~600 images. At this point if you have the folder displayed in Finder with preview on, you'll see the new transparent images as they are processed.

Okay now that I'm typing all this out I'm thinking, WTF is wrong with me? But it actually worked very well and fast! Now it is very easy for me to repeat this process and I have re-used these Automator workflows many times.

It was actually very good that I recorded the process while it was still somewhat fresh, in case I ever have the need to repeat the process.

Note: I have included a link to my Automator workflows for anyone that wants to use them. If you do happen to get it to work with files / path names with spaces in them, please let me know. Thanks

No comments:

Post a Comment