You are on page 1of 7

21/05/13

Writing Python script for Gimp | Chobo Programmer

ChoboProgrammer
Blog about different technical stuff.

WritingPythonscriptforGimp
PostedonDecember17,2010

Yesterday I needed to create a bunch of identical icons, but with different 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


choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/ 1/7

21/05/13

Writing Python script for Gimp | Chobo Programmer

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): # ! / u s r / b i n / e n v p y t h o n f r o m g i m p f u i m p o r t * i m p o r t o s r u n ( * a r g s ) : " " " G e n e r a t e d i c o n s w i t h d i f f e r e n t n u m b e r s u s i n g t e m p l a t e " " " t e m p l a t e _ n a m e , n u m , p r e f i x , o u t p u t _ d i r = a r g s o u t p u t _ d i r = o s . p a t h . e x p a n d u s e r ( ' ~ / ' + o u t p u t _ d i r ) i f n o t o s . p a t h . e x i s t s ( o u t p u t _ d i r ) : o s . m a k e d i r s ( o u t p u t _ d i r ) f o r i i n x r a n g e ( 1 , n u m + 1 ) : i m = p d b . g i m p _ f i l e _ l o a d ( t e m p l a t e _ n a m e , t e m p l a t e _ n a m e ) # S u p p o s e t h e n a m e o f l a y e r i n ' T e x t ' t e x t _ l a y e r = f i l t e r ( l a m b d a x : x . n a m e = = ' T e x t ' , i m . l a y e r s ) [ 0 ] p d b . g i m p _ t e x t _ l a y e r _ s e t _ t e x t ( t e x t _ l a y e r , s t r ( i ) ) # W e c a n s a v e o n l y o n e l a y e r a t t i m e # M e r g e r v i s i b l e l a y e r s i n o n e m e r g e d = p d b . g i m p _ i m a g e _ m e r g e _ v i s i b l e _ l a y e r s ( i m , 0 ) o u t p u t _ f i l e n a m e = " % s % d . p n g " % ( p r e f i x , i ) f u l l _ o u t p u t _ f i l e n a m e = o s . p a t h . j o i n ( o u t p u t _ d i r , o u t p u t _ f i l e n a m e ) p d b . f i l e _ p n g _ s a v e _ d e f a u l t s ( i m , m e r g e d , f u l l _ o u t p u t _ f i l e n a m e , f u l l _ o u t p u t _ f i l e n a m e ) p r i n t " F i n i s h e d " r e g i s t e r ( " c h o b o _ i c o n _ g e n " , " " , " " , " " , " " , " " , " < T o o l b o x > / X t n s / L a n g u a g e s / P y t h o n F u / _ C h o b o S c r i p t s / _ I c o n G e n e r a t o r " , " " , [ ( P F _ F I L E , " a r g 0 " , " T e m p l a t e f i l e " , " " ) , ( P F _ I N T , " a r g 1 " , " M a x i m u m n u m b e r o f i c o n s t o c r e a t e " , 2 9 ) ,

choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/

2/7

21/05/13

Writing Python script for Gimp | Chobo Programmer

( P F _ S T R I N G , " a r g 3 " , " O u t p u t f i l e p r e f i x " , " " ) , ( P F _ S T R I N G , " a r g 4 " , " O u t p u t d i r e c t o r y ( r e l a t i v e t o u s e r ' s h o m e ) " , " " ) , ] , [ ] , r u n ) m a i n ( )

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)

choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/

3/7

21/05/13

Writing Python script for Gimp | Chobo Programmer

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 with python-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. i m = p d b . g i m p _ f i l e _ l o a d ( t e m p l a t e _ n a m e , t e m p l a t e _ n a m e ) 2. Finds a layer, named Text, and changes its text to some value. t e x t _ l a y e r = f i l t e r ( l a m b d a x : x . n a m e = = ' T e x t ' , i m . l a y e r s ) [ 0 ] p d b . g i m p _ t e x t _ l a y e r _ s e t _ t e x t ( t e x t _ l a y e r , s t r ( i ) ) 3. Merges the image to a single layer (required for saving) m e r g e d = p d b . g i m p _ i m a g e _ m e r g e _ v i s i b l e _ l a y e r s ( i m , 0 ) 4. Saves the image to a specific location. p d b . f i l e _ p n g _ s a v e _ d e f a u l t s ( i m , m e r g e d , f u l l _ o u t p u t _ f i l e n a m e , f u l l _ o u t p u t _ f i l e n a m e ) 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.

" c h o b o _ i c o n _ g e n " ,

" " ,

" " ,

" " ,

" " ,

" " ,

Next line, is menu path to your script. The underscore here is a shortcut key (Useful, if you hate using a mouse).

" / X t n s / L a n g u a g e s / P y t h o n - F u / K i l l e r S c r i p t s / _ I c o n

G e n e r a t o r " ,

" " ,

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).

choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/

4/7

21/05/13

Writing Python script for Gimp | Chobo Programmer

[ ( P F ( P F ( P F ( P F ] , _ F I _ I N _ S T _ S T L E , " a r T , " R I N G , " R I N G , " g 0 " a r g a r g a r g , 1 " , 3 " , 4 " , " T e m " M " O " O p l a a x i u t p u t p t e f i m u m n u t f i u t d i l e " u m b l e r e c , " " ) , o f i c o n s t o p r e f i x " , " " ) , t o r y ( r e l a t i v e e r c r e a t e " , t o u s e r ' s 2 9 ) , h o m e ) " , " " ) ,

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 in pdb 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

choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/

5/7

21/05/13

Writing Python script for Gimp | Chobo Programmer

GitHub
I shared the script and the icon template on GitHub here. Feel free to play with it. I hope itll be useful.
References:

Gimp Batch Image Editing using Script-Fu & Gimp Python script registration parameters Python Scripting in Gimp
Abouttheseads

Ratethis:

1Vote

Likethis:

Like
Onebloggerlikesthis.

Thisentrywaspostedinprogramming,pythonandtaggedgimp,python,script.Bookmarkthepermalink.

2ResponsestoWritingPythonscriptforGimp
johnwun says:
February3,2013at4:24pm

Nice intro Just the right amount of instruction to get started Thanks!
Reply

wakatana says:
February20,2013at9:37pm

Hello, I am interested in scripting gimp so I try your code. What I have done is described here: http://pastebin.com/i18wgLe8 but 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

ChoboProgrammer
Theme: Twenty Ten Blog at WordPress.com.

choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/

6/7

21/05/13

Writing Python script for Gimp | Chobo Programmer

choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/

7/7

You might also like