Manipulating image files by batch

Started by
11 comments, last by Servant of the Lord 9 years, 5 months ago

Hello all,

I have several hundred sprites in .bmp format I need export to .gif and set the background color to transparent. Doing this manually with each file would be cumbersome. Is there an easy-to-use open source graphics editor that can do this in a batch mode? I believe it is possible with Gimp, but I'm having some difficulty figuring out how the batch script would look.

Any help would be appreciated.

Thanks.

Corey

Advertisement

http://www.imagemagick.org/script/convert.php

You can use gimp's python/scheme console and write a batch script. Alternatively, you can use David's Batch Processor plugin.
If you have to use GIMP, in my experience the Python scripting is easier than the Scheme. The documentation is awful for either, and you need a lot more effort in order to understand how it works.

In any case, it should be something like this untested script.
First you'd paste this script into Notepad and fill-in the path to the input directory, as well as the background colour to be removed for transparency.
Then copy the modified script and paste it on the Python console from within GIMP, to execute it.

import os
from gimpfu import *

# FILL THIS IN:
inputDir = "C:\\project\\graphics\\" # Note the double slash.
bgColour = (255, 0, 255) 

for file in os.listdir( inputDir )
    try:
        inputFile = inputDir + file
      
        image = pdb.file_bmp_load( 0, inputFile, inputFile )
        pdb.gimp_selection_none( image )
       
        tempLayer = image.layers[0]
        pdb.add_layer_alpha( tempLayer ) # Add an alpha channel to the main layer.
        pdb.by_color_select( tempLayer, bgColour, 0, 2, 0, 0, 0.0, 0) # Select all the background pixels.
        pdb.gimp_edit_clear( tempLayer ) # Clear the background pixels so they're transparent.
               
        outputFile = inputFile.replace( "bmp", "gif" )
        pdb.file_gif_save( tempLayer, outputFile, outputFile, 0, 0, 0, 0 )

        gimp.delete( image )

    except Exception as err:
        gimp.message( "Error: " + str( err ) )
Before writing this I went through Google and found these as reference:

- http://hs.riverdale.k12.or.us/~pnelson/web_design/transparent.html
- http://pastebin.com/jCEKQugN
- http://www.gimp.org/docs/python/

This gives me some good info to work with. Thanks!

Removing a solid background often doesn't work very well, leaving "dirty" pixels around the edge of the image, or accidentally cutting holes in the image if there is anything the same color as the background. So be sure you examine the images after the batch conversion.

I want to help design a "sandpark" MMO. Optional interactive story with quests and deeply characterized NPCs, plus sandbox elements like player-craftable housing and lots of other crafting. If you are starting a design of this type, please PM me. I also love pet-breeding games.

Removing a solid background often doesn't work very well, leaving "dirty" pixels around the edge of the image, or accidentally cutting holes in the image if there is anything the same color as the background. So be sure you examine the images after the batch conversion.

That's an excellent point, and after some experimentation, I find it leaves some jagged edges around the sprite. Probably not an issue at low res, but maybe something I'll need to address anyway. This is what happens when a programmer tries to take up graphic arts. smile.png

The GIF format uses indexed colour and bit-transparency.
If your original graphics have smooth, antialiased edges it will look surprising.

Removing a solid background often doesn't work very well, leaving "dirty" pixels around the edge of the image, or accidentally cutting holes in the image if there is anything the same color as the background. So be sure you examine the images after the batch conversion.

That's an excellent point, and after some experimentation, I find it leaves some jagged edges around the sprite. Probably not an issue at low res, but maybe something I'll need to address anyway. This is what happens when a programmer tries to take up graphic arts. smile.png

It seems like every artist who is getting started with raster art programs and/or scanning drawings into them runs into this problem, it's pretty universal. The concept "digital inking" is the usual solution for scanning pencil drawings. For colored work, basically you don't scan that at all, you create it digitally in a transparent layer (generally in a psd or xcf file), under which you can put an easily removable background layer. Then to make the sprite you flatten your layers into a PNG, which retains transparency but not layering.

I want to help design a "sandpark" MMO. Optional interactive story with quests and deeply characterized NPCs, plus sandbox elements like player-craftable housing and lots of other crafting. If you are starting a design of this type, please PM me. I also love pet-breeding games.

ImageMagick is what I've used for bulk scaling, cropping, format conversions, and others as well.

Command for converting BMP files to GIF files:

//Warning: Make sure you set your command-line's current directory to the directory (and sub-directories) you want to crawl before executing this command.
//Note: It won't delete the original BMP files, so you need to do that afterward - easy enough to do in Windows Explorer by searching for *.bmp and selecting all).
for /r %i in (*.bmp) do mogrify -format gif -transparent #FF0000 "%i"

Replace '#FF0000' with your actual background color. Make sure you have your current directory set - you don't want to crawl over your entire computer!

This is Windows command line command saying to recursively crawl the current directory, filtering by *.bmp and executing the command that follows, replacing %i with the filepath and filename.


for /r %i in (*.bmp) do

This is an ImageMagick command saying to change the file "%i" into a gif, and make "#FF000000" the transparent part of the gif.


mogrify -format gif -transparent #FF0000 "%i"

This topic is closed to new replies.

Advertisement