script gimp
TRANSCRIPT
-
8/22/2019 Script Gimp
1/7
21/05/13 Writing Python script for Gimp | Chobo Programmer
choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/ 1/7
Writing Python script for GimpPosted on December 17, 2010
Yesterday I needed to create a bunch of identical icons, but with d ifferent numbers. I still havent come up with a
final design of the icons, but I needed something to put in my application. So I decided to create really simple
template icon in Gimp (open-source image editor, aka Free Photoshop) and then just manually edit and create all
the icons.
When I created 3 images, I realized that, if I will need to change the design of icons, I would have to manually
create all the icons again (In total I need 29 icons, dont ask me why). Thats a crappy work that requires some
automation.
I started to google the way to script it, and I found out that I can write a script in Python. And that was super
great, because I love Python .
The process of scripting Gimp is really straightforward, but requires some googling to build a complete picture (I
will try to save you some time with this post):
1. Create directory for your scripts
2. Tell Gimp where to look for your scripts
3. Create crappy-job-automatization-script.py in the directory.
4. Run terminal and tell GIMP what to do.
Below I will describe the process in details. Im doing all this on Mac, but I believe that differences are not very
significant.
1. Creating directory for your scripts
Chobo Programmer
http://choboprogrammer.files.wordpress.com/2010/12/gimp-python.pnghttp://choboprogrammer.files.wordpress.com/2010/12/gimp-python.pnghttp://choboprogrammer.files.wordpress.com/2010/12/gimp-python.pnghttp://choboprogrammer.files.wordpress.com/2010/12/gimp-python.pnghttp://choboprogrammer.files.wordpress.com/2010/12/gimp-python.pnghttp://choboprogrammer.wordpress.com/http://choboprogrammer.files.wordpress.com/2010/12/1_2_3.pnghttp://choboprogrammer.files.wordpress.com/2010/12/gimp-python.pnghttp://www.gimp.org/http://choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/http://choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/ -
8/22/2019 Script Gimp
2/7
21/05/13 Writing Python script for Gimp | Chobo Programmer
choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/ 2/7
I really like Unix approach to store application settings as hidden files inside a users home directory. So lets
create a hidden directory for Gimp scripts. Usually, its .gimp2-6/scripts, but I dont like to explicitly write
application version, so I just named it .gimp/scripts.
Run in terminal:
mkdir -p ~/.gimp/scripts
2. Telling Gimp where to look for your scripts
Run Gimp
Select from menu Edit->Preferences
Expand Folders
Select Scripts and add your scripts directory
Note: You probably noticed that there is already created folder for scripts in /Library. You can use it without any
problems, just I like when everything is in my home directory.
3. Creating icon_generator.py in the directory.
In the script directory create a file and insert the following code (You can find an updated version of the script on
GitHub):
/usr/bin/env pythonmgimpfu import*ortos
run(*args):
"""Generated icons with different numbers using template""" template_name, num, prefix, output_dir =args
output_dir =os.path.expanduser('~/'+output_dir) ifnotos.path.exists(output_dir): os.makedirs(output_dir)
fori inxrange(1, num+1): im =pdb.gimp_file_load(template_name, template_name)
# Suppose the name of layer in 'Text' text_layer =filter(lambdax: x.name =='Text', im.layers)[0] pdb.gimp_text_layer_set_text(text_layer, str(i))
# We can save only one layer at time # Merger visible layers in one merged =pdb.gimp_image_merge_visible_layers(im, 0)
output_filename ="%s%d.png"%(prefix, i) full_output_filename =os.path.join(output_dir, output_filename)
pdb.file_png_save_defaults(im, merged, full_output_filename, full_output_filenam
print"Finished"
ister( "chobo_icon_gen", "", "", "", "", "",
"/Xtns/Languages/Python-Fu/_Chobo Scripts/_Icon Generator", "", [ (PF_FILE, "arg0", "Template file", ""), (PF_INT, "arg1", "Maximum number of icons to create", 29),
https://github.com/hwangroman/gimp-python/blob/master/icon_generator.py -
8/22/2019 Script Gimp
3/7
21/05/13 Writing Python script for Gimp | Chobo Programmer
choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/ 3/7
I will provide some analysis of the script later.
4. Running the script
You have two ways for running the script:
1. From GUI.
2. From terminal in batch mode.
1. Running from GUI
From menu select Filter -> Python-Fu -> Chobo Scripts -> Icon Generator, and youll see following dialog
window.
This is where all that arguments stuff shines. During the registration you told GIMP that Template file argument
is of FILE type, and now Gimp renders a corresponding control, that allows you to pick a file. Now select a
template file, and run.
Note: You dont have to restart Gimp, in order to apply the script changes. Nice.
2. Running from terminal
First, you must find the actual Gimp executable. There are two ways to obtain Gimp: build using MacPorts and
download from official web site. If youve used the latter method, like me, you can find gimp in
/Applications/Gimp.app/Contents/Resources/bin (If not, youre gimp is already in PATH).
cd /Applications/Gimp.app/Contents/Resources/bin
./gimp no-interface verbose batch (python-fu-chobo-icon-gen RUN-NONINTERACTIVE/path/to/your/icon_template.xcf 29 icon- output) batch (gimp-quit 1)
(PF_STRING, "arg3", "Output file prefix", ""), (PF_STRING, "arg4", "Output directory (relative to user's home)", ""), ], [], run )
n()
http://www.gimp.org/http://choboprogrammer.files.wordpress.com/2010/12/screen-shot-2010-12-16-at-7-09-38-pm.png -
8/22/2019 Script Gimp
4/7
21/05/13 Writing Python script for Gimp | Chobo Programmer
choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/ 4/7
As you can see, we invoked your script using python-fu-chobo-icon-gen function. Actually, here were using a
Scheme syntax, the other language, that you can use for Gimp Scripting. So its just a simple function call with 5
arguments. First argument (RUN-NONINTERACTIVE) just tells Gimp, that were running the script no from
GUI, and is ignored by scripts.
The name, we used for registration (chobo_icon_gen), is prefixed withpython-fu-, and modified to conform
Scheme syntax.
The next batch command tells Gimp to quit.
Script Analysis
Method run executes all logic of the script, lets analyse it in some details. It accepts 4 arguments:
template filename
number of icons to generate. Script will generate icons in range [1, 29].
output file prefix, which will be added to each generated icon filename.output directory. Directory will be created if case it doesnt exist.
In order to generate one icon the script does following:
1. Loads the template image.
2. Finds a layer, named Text, and changes its text to some value.
3. Merges the image to a single layer (required for saving)
4. Saves the image to a specific location.
To make your script visible (and runnable) by Gimp you need to register it. The second half of the script is the
script registration. First line contains the function name, followed by 5 does-not-really-matter parameters.
"chobo_icon_gen", "", "", "", "", "",
Next line, is menu path to your script. The underscore here is a shortcut key (Useful, if you hate using a mouse).
"/Xtns/Languages/Python-Fu/KillerScripts/_Icon Generator", "",
Next is a list of input arguments, which is self-explanatory. I tool me a while to find all available parameter types
(You can find them here).
im =pdb.gimp_file_load(template_name, template_name)
text_layer =filter(lambdax: x.name =='Text', im.layers)[0]pdb.gimp_text_layer_set_text(text_layer, str(i))
merged =pdb.gimp_image_merge_visible_layers(im, 0)
pdb.file_png_save_defaults(im, merged, full_output_filename, full_output_filename)
http://oldhome.schmorp.de/marc/man/Gimp/Fu.html#parameter%20types -
8/22/2019 Script Gimp
5/7
21/05/13 Writing Python script for Gimp | Chobo Programmer
choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/ 5/7
[
(PF_FILE, "arg0", "Template file", ""),
(PF_INT, "arg1", "Maximum number of icons to create", 29),
(PF_STRING, "arg3", "Output file prefix", ""),
(PF_STRING, "arg4", "Output directory (relative to user's home)", ""),
],
The last argument is out actual Python function to be called.
Advices:
Start small
When writing a script, first start with small steps: create a really dumb script (put a single print Working), and
see if it is working. I killed a lot of time, tracing typos in my Python code. Then I started all over again using
sample from Python Scripting in GIMP documentation.
Use console
Python console in GIMP is a really nice feature, that greatly helps writing Python scripts. You can find it at menu
Filters -> Python-Fu -> Console. I was really struggling to find a decent documentation of Gimp Scripting API,
until I found a Python Procedure Browser, that solved my problems.
You can find the browser by clicking Browse on Python console (Not really intuitive, I would say). You can ask
but how can I actually call these functions in my Python code?. Just double click on a method, that youd like to
use, and it will appear in console. And you will see that all the functions are located inpdb namespace (of course,
all those ugly minuses (-) were replaced with underscores (_)).
I recommend the following process for writing a script.
1. Play with functions in Gimps Python console.
2. Change one line in the script
3. Run the script in batch mode.4. Go back to the console
5. Continue until youre done
http://choboprogrammer.files.wordpress.com/2010/12/screen-shot-2010-12-16-at-4-41-26-pm1.pnghttp://www.gimp.org/docs/python/index.html#script_fu_interface -
8/22/2019 Script Gimp
6/7
21/05/13 Writing Python script for Gimp | Chobo Programmer
choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/ 6/7
Rate this: 1 Vote
Like this:
One blogger likes this.
GitHub
I shared the script and the icon template on GitHub here. Feel free to play with it. I hope itll be useful.
References:
GimpBatch Image Editing using Script-Fu & Gimp
Python script registration parameters
Python Scripting in Gimp
This entry was posted in programming, python and tagged gimp, python, script. Bookmark the permalink.
2 Responses to Writing Python script for Gimp
Chobo Programmer
About t hese ads
johnwunsays:
February 3, 2013 at 4:24 pm
Nice intro Just the right amount of instruction to get started Thanks!
Reply
wakatanasays:
February 20, 2013 at 9:37 pm
Hello, I am interested in scripting gimp so I try your code. What I have done is described here:
http://pastebin.com/i18wgLe8but nothing happened, I also trying to change from chobo_icon_gen to
chobo_icon_gen2 to see if I get some error about undefined function, but I get the same message: GNU Image
Manipulation Program version 2.6.12 and it all ends viwth return code 0. Many thanks
Reply
Theme: Twenty Ten Blog at WordPress.com.
http://wordpress.com/?ref=footerhttp://theme.wordpress.com/themes/twentyten/http://choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/?replytocom=21#respondhttp://pastebin.com/i18wgLe8http://choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/#comment-21http://choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/?replytocom=20#respondhttp://choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/#comment-20http://gravatar.com/johnwunhttp://en.wordpress.com/about-these-ads/http://choboprogrammer.wordpress.com/http://choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/http://choboprogrammer.wordpress.com/tag/script/http://choboprogrammer.wordpress.com/tag/python/http://choboprogrammer.wordpress.com/tag/gimp/http://choboprogrammer.wordpress.com/category/python/http://choboprogrammer.wordpress.com/category/programming/http://www.gimp.org/docs/python/index.html#script_fu_interfacehttp://oldhome.schmorp.de/marc/man/Gimp/Fu.html#parameter%20typeshttp://chriscrossx.blogspot.com/2009/04/batch-image-editing-using-script-fu.htmlhttp://www.gimp.org/https://github.com/hwangroman/gimp-pythonhttp://en.gravatar.com/vanessawdihttp://widgets.wp.com/likes/# -
8/22/2019 Script Gimp
7/7
21/05/13 Writing Python script for Gimp | Chobo Programmer
choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/ 7/7