1

I've large set of images. I wan't to chage their background to specific color. Lets say green. All of the images have transparent background. Is there a way to perform this action using python-fu scripting in Gimp. Or some other tool available to do this specific task in automated fashion.

Red Devil
  • 349
  • 4
  • 16
  • check this out http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=10665 – Leo Aug 14 '14 at 14:41

2 Answers2

0

Yes there is - Although this question is not an exact duplicate, i is not practical to just type in the basics of creating a Python plug-in every time someone asks to automate some task in the GIMP

I will have to ask you to look at GIMP: Create image stack from all image files in folder and maybe some other python-fu related answers for the basics.

After you get a simple "hello world" script going, simply register a script that asks for a string with the desired folder, use Python's os.listdir, or glob.glob to fetch the paths to the iamge files, and simply loop through them repeating calls to:

image = pdb.gimp_file_load (...)
image.new_layer(pos=1, fill_mode = FOREGROUND_FILL) 
pdb.gimp_file_save(...)
pdb.gimp_image_delete(image)

The parameters to the PDB calls are easy to check at GIMP's help procedure browser - the image's new_layermethod is not really documented, and can replace 3-4 pdb calls - the possible parameters to it are: "name", "width", "height", "offset_x", "offset_y", "alpha", "pos", "opacity", "mode", "fill_mode",

all of which are optional. "pos" is the layer position: 0 is at the top of the image. "1" would be just bellow the topmost layer.

It should be clear that the gimp_image_delete call will just remove the image from memory - not the file from disk. Simply de-referencing it on the Python side won't make GIMP forget it. Likewise, if you want to interact with any image open in this way, you have to call pdb.gimp_display_new for that image.

Community
  • 1
  • 1
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • Thanks jsbueno. I get how to process batch of files using python-fu. But I don't know how to modify transparent background to some solid color. – Red Devil Aug 16 '14 at 03:00
  • You have to add another layer, behind the one that is loaded, with the desired solid color. The `image.new_layer` call in the answer does that - you have to set-up the contest Foreground color with a call to `pdb.gimp_context_set_foreground` before calling it, tough. – jsbueno Aug 16 '14 at 18:27
0

You should be able to accomplish this by simply setting the background color to your desired color and then simply flatten the image which will make all transparent areas become set to the current background color.

pdb.gimp_palette_set_background( 'green' )
image     = pdb.gimp_file_load ('myImage.png')
flatLayer = pdb.gimp_image_flatten( image )
pdb.gimp_file_save( image, flatLayer, 'myFlatFile.png' , 'myFlatFile.png')
jxramos
  • 7,356
  • 6
  • 57
  • 105