Jump to content
  • Advertisement
Sign in to follow this  
iac249

Manipulating image files by batch

This topic is 1494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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/

Share this post


Link to post
Share on other sites

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.

Edited by sunandshadow

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
The GIF format uses indexed colour and bit-transparency.
If your original graphics have smooth, antialiased edges it will look surprising.

Share this post


Link to post
Share on other sites

 

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.

Share this post


Link to post
Share on other sites

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"
Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!