It is currently 21 Jan 2018, 17:56

GIMP Python Fu ScriptsWriting Python-Fu Scripts for GIMP (Showcase | Slideshow)

Share your Python Fu scripts for GIMP here
Post Reply
User avatar
tim

Administrator
View: Showcase | Slideshow
Topic Posts: 3/6
Posts: 2071
Joined: 25 Sep 2016, 17:16
Cash on hand: 7,180.19 GLP
Bank: 42,250.25 GLP
Gender: Guy
Location: Victoria, BC
Website: https://gimplearn.net
Operating System: Windows
GIMP Version: 2.8.22
My Personal Blog
My FineArtAmerica
x 1106
x 240
Contact:
Canada

Writing Python-Fu Scripts for GIMP

Post#1 by tim » 25 Oct 2016, 14:44


This is a short tutorial that hopefully gets you started on your way on writing Python-Fu Scripts for GIMP.
This tutorial assumes that you have some knowledge of Python programming language.

You can start your first script by using the empty template that basically has the basic structure of a Python-Fu Script here -> Sample Shell Template Python Fu Script for GIMP

1. Open that .py file up in a text editor (I use NotePad++ on windows).

2. Change the function name from python_sample_shell to any name you'd like but preferably starts with python_
and change the last parameter of the register call to call the same name (for example. python_my_first_script). (Line 32 and line 67)

3. Change your menu location for your script to be able to be run from by changing the string

Code: Select all

"<Image>/Python-Fu/Sample Shell",             #Menu 
path[/i][/b] (Line 51) to wherever you like
maybe you want it to be under

Code: Select all

"<Image>/Python-Fu/My First Script"
for example.

4. Change Author name currently "Tin Tran" to your own name (line 4, line 48 and line 49)

5. Give the script a description (line 46, line 47)

6. Change the name that this script will register under for other scripts to call it by (line 45). Preferably to start with python_fu_ so that people know it's written in python fu when they browse for it in GIMP's procedure browser.

7. Now save the file and put it in your GIMP's /plug-ins/ folder. Restart GIMP. And you should be able to see it registered under under wherever you changed it to in step 3.

8. Now you try running it, it doesn't do anything other than printing the message "This is sample shell script." to GIMP's Error console window. Because we haven't added any code for it to run yet.

9. You be adding your code on line 38 (below line 37 that says #PUT YOUR CODE HERE).

10. In GIMP, go to the "Help" menu and click on "Procedure browser", and you'll be shown a list of all the procedures you can call from your script.
Procedure names are a series of words separated hyphens (-). As an example, type the string selection-all into the Search box, and you'll see the results is narrowed down to show one procedure named gimp-selection-all.

11. Click on the shown gimp-selection-all procedure, and on the right hand side it shows you details about this procedure.
It takes one parameter which is an image.

12. To call this procedure in python we have to use the class pdb I think it stands for procedure database and the name in pythons are exactly the same as shown in procedure browser except replace hyphens(-) with under scores (because in python the hyphen means minus sign). So to call the procedure we see in step 11. add the below code (don't forget to indent with a single tab, since other lines in our .py file are indented by tab).
[/code]pdb.gimp_selection_none(image)[/code]

13. With python scripts, once the script is registered (without change parameters to calling our function), we don't have to restart GIMP, we can just save the code and run the script from GIMP and changes take affect right away.

14. You'll see that if you run script now from GIMP, the whole image is selected like what we told it to do.

15. The example shown had no return parameters.

16. With procedures that do return parameters, you can set a variable in python to save the returned value. For example if you look up gimp-context-get-foreground procedure, it returns a single color we can use code like below

Code: Select all

	mycolor = pdb.gimp_context_set_foreground()
and now mycolor should contain the foreground color returned by that procedure call.

17. For procedures that return multiple values for example: gimp-vectors-stroke-get-points which returns 4 return values, you can just specify 4 variables separated by commas to equal to the procedure call like below and the returned values will be saved in them.

Code: Select all

	type,num_points,controlpoints,closed = pdb.gimp_vectors_stroke_get_points(vectors,stroke_id)
This code is just a sample of what you can do, obviously you'd have to get vectors, and stroke_id first in order to call this procedure. And we currently don't have those values set in our python function.

18. That is it for now, hopefully it was enough to get you started on some simple scripts, if you run into trouble don't hesitate to ask.
0 x



aka Tin
Image gimplearn.net                                              

Registered members enjoy ads-free content
User avatar
Wallace
Forum Member
View: Showcase | Slideshow
Topic Posts: 3/6
Posts: 192
Joined: 11 Oct 2016, 14:24
Cash on hand: 1,332.62 GLP
Gender: Guy
Location: Arizona USA
Website: http://gimpchat.com
Operating System: Windows
GIMP Version: 2.8.18
My YouTube Channel
My deviantart Account
x 3
x 29
Contact:
Gimp Nation

Post#2 by Wallace » 25 Oct 2016, 15:30


Thank you. :geek:
0 x




User avatar
tim

Administrator
View: Showcase | Slideshow
Topic Posts: 3/6
Posts: 2071
Joined: 25 Sep 2016, 17:16
Cash on hand: 7,180.19 GLP
Bank: 42,250.25 GLP
Gender: Guy
Location: Victoria, BC
Website: https://gimplearn.net
Operating System: Windows
GIMP Version: 2.8.22
My Personal Blog
My FineArtAmerica
x 1106
x 240
Contact:
Canada

Post#3 by tim » 25 Oct 2016, 16:00


does it mean you're gonna try to get into some scripting action too?
0 x



aka Tin
Image gimplearn.net                                              

User avatar
Wallace
Forum Member
View: Showcase | Slideshow
Topic Posts: 3/6
Posts: 192
Joined: 11 Oct 2016, 14:24
Cash on hand: 1,332.62 GLP
Gender: Guy
Location: Arizona USA
Website: http://gimpchat.com
Operating System: Windows
GIMP Version: 2.8.18
My YouTube Channel
My deviantart Account
x 3
x 29
Contact:
Gimp Nation

Post#4 by Wallace » 25 Oct 2016, 16:27


admin wrote:does it mean you're gonna try to get into some scripting action too?
Not realy...
aw shuckspurple.gif
aw shuckspurple.gif # (1.15 KiB) Viewed 1123 times (Watermarked | Thumbnail)
0 x




User avatar
tim

Administrator
View: Showcase | Slideshow
Topic Posts: 3/6
Posts: 2071
Joined: 25 Sep 2016, 17:16
Cash on hand: 7,180.19 GLP
Bank: 42,250.25 GLP
Gender: Guy
Location: Victoria, BC
Website: https://gimplearn.net
Operating System: Windows
GIMP Version: 2.8.22
My Personal Blog
My FineArtAmerica
x 1106
x 240
Contact:
Canada

Post#5 by tim » 25 Oct 2016, 16:33


Lol
0 x



aka Tin
Image gimplearn.net                                              

User avatar
Wallace
Forum Member
View: Showcase | Slideshow
Topic Posts: 3/6
Posts: 192
Joined: 11 Oct 2016, 14:24
Cash on hand: 1,332.62 GLP
Gender: Guy
Location: Arizona USA
Website: http://gimpchat.com
Operating System: Windows
GIMP Version: 2.8.18
My YouTube Channel
My deviantart Account
x 3
x 29
Contact:
Gimp Nation

Post#6 by Wallace » 25 Oct 2016, 16:36


You made it so... :aw_shucks_purple:
0 x




Post Reply

Last 20 Members Who Visited This Topic

User avatar animicule (1)

  • Similar Topics
    Replies
    Views
    Last post
• Featuring Members' Works (Image Attachments Only) •
1: dinasset
2: RJKD
3: animicule
4: oldmangrumpy
5: tim
6: oldmangrumpy
7: Conbagui

Who is online

Users browsing this forum: No registered users and 3 guests