It is currently 20 Feb 2018, 23:12

GIMP TutorialsHow to write Plug-in for GIMP in Python (Showcase | Slideshow)

GIMP Tutorials
User avatar
tim

Administrator
View: Showcase | Slideshow
Topic Posts: 10/18
Posts: 2162
Joined: 26 Sep 2016, 00:16
Cash on hand: 7,946.37 GLP
Bank: 51,356.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 1207
x 258
Contact:
Canada

How to write Plug-in for GIMP in Python

Post#1 by tim » 11 Oct 2017, 15:43


You're gonna be able to write your own scripts after this tutorial.
First, use a text editor to save the below code as a .py file (ie: sample.py).

Code: Select all

#!/usr/bin/env python

# sample.py Rel 1
# This is a sample script for practicing writting plug-ins for GIMP
# Created by Tin Tran
# Comments directed to https://gimplearn.net
#
# License: GPLv3
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# To view a copy of the GNU General Public License
# visit: http://www.gnu.org/licenses/gpl.html
#
#
# ------------
#| Change Log |
# ------------
# Rel 1: Initial release

from gimpfu import *
def python_sample(image, layer) : #FUNCTION DEFINITION
	pdb.gimp_image_undo_group_start(image)
	pdb.gimp_context_push()
	#YOUR CODE BEGINS=======================
	pdb.gimp_message("Hello World")
	
	#YOUR CODE ENDS ========================
	pdb.gimp_context_pop()
	pdb.gimp_image_undo_group_end(image)
	pdb.gimp_displays_flush()
    #return

register(
	"python_fu_sample",
	"Sample Python Fu Script",
	"Sample Python Fu Script...",
	"Tin Tran",
	"Tin Tran",
	"Oct 2017",
	"<Image>/Python-Fu/Sample...",
	"*",      # Create a new image, don't work on an existing one
	[ 
	#INPUT BEGINS
	#(PF_OPTION, "arrow_side", "Arrows Ends:", SIDE_END, SIDE_NAMES),
	#(PF_TOGGLE, "arrow_close", "Arrows Close:", 0),
	#(PF_SPINNER, "border_width", "Border Width (bevelled width):", 10, (0, 500, 1)),
	#(PF_SPINNER, "shadow_offset_x", "Shadow Offset X:", 6, (-4096,4096,1)),
	#(PF_SPINNER, "shadow_offset_y", "Shadow Offset Y:", 6, (-4096,4096,1)),
	#(PF_SPINNER, "shadow_blur_radius", "Shadow Blur Radius:", 15, (0,1024,1)),
	#(PF_SPINNER, "shadow_opacity", "Shadow Opacity:", 100, (0,100,1)),
	#INPUT ENDS
	],
	[],
	python_sample)

main()
And save the .py file in your GIMP's /plug-ins/ folder.
If you restart GIMP now you'll have Python-Fu/Sample menu where if you click that to run script, you'll see "Hello World" printed to error console (which can be opened by clicking Windows/Dockable Dialogs/Error Console in GIMP).

In the above code, any text after the hash (#) is a comment in Python (where you can enter anything you'd like to document your code).

If you change your function name

Code: Select all

def python_sample(image, layer) : #FUNCTION DEFINITION
for example, from python_sample to python_my_first_script, you'll also have to change the last parameter of the register call to that same name so that the script knows to call your function when it's called (when you run it).
Also change the first parameter of register call to something that represents your function too for example "python_fu_my_first_script".

If you want know what functions are available to you: in GIMP, click on Help/Procedure Browser and you look at/search for any function available to you.

Just when you call it in your script you'll have to add "pdb." in front of the procedure name and convert all the hyphens to underscores (_).

For example, if you'll look up gimp-message, you'll see that it is a function found in procedure browser that takes one parameter.

In our code we call it by using pdb.gimp_message() and gave it "Hello World" as the parameter.

Similarly, you can call other functions found in procedure browser.

If you wanted to provide user with other input options, look at the portion between

Code: Select all

#INPUT BEGINS
and

Code: Select all

#INPUT ENDS
You'll see some commented out input parameters, you can try to uncomment out of them out to use, but make sure you'll also add the input into
function definition so that you can use this input in your script.
For example if you uncommented one of them out,
you might change your function definition to

Code: Select all

def python_sample(image, layer, my_first_param) : #FUNCTION DEFINITION
Of course my_first_param could be named anything as long as it's a valid variable name in Python and then you can use it in your script.

Leave the first 2 parameters as image and layer as those are automatically passed in when the script runs it passes those in to your function call.
You can use those parameter, as the image and layer are active/selected image/layer when your script is called.
Only add your parameters after these 2 parameters.

If you want to add other types of inputs, you can look at http://registry.gimp.org/files/demo-plugin-GUI.py to see example of how inputs are declared, pick a type and try adding it into script in between the #INPUT BEGINS and #INPUT ENDS portion of code and add it to your function parameter.

That's about all I wanted to share on how to write your first Python plug-in for GIMP.
If you have other questions, please reply to this topic with your question and I'll try to answer with all I know.
Cheers.

1 x



aka Tin
Image gimplearn.net                                              

Registered members enjoy ads-free content
User avatar
RJKD
Tutorial Creator
View: Showcase | Slideshow
Topic Posts: 3/18
Posts: 1353
Joined: 03 Oct 2016, 13:01
Cash on hand: 10,349.49 GLP
Bank: 0.00 GLP
Gender: Guy
Location: Sheffield UK
Operating System: Windows
GIMP Version: 2.8.20
x 1490
x 210
Contact:
Great Britain

Post#2 by RJKD » 11 Oct 2017, 15:52


Thanx a lot T 😉
1 x




User avatar
tim

Administrator
View: Showcase | Slideshow
Topic Posts: 10/18
Posts: 2162
Joined: 26 Sep 2016, 00:16
Cash on hand: 7,946.37 GLP
Bank: 51,356.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 1207
x 258
Contact:
Canada

Post#3 by tim » 11 Oct 2017, 15:53


You're welcome and remember do not hesitate to ask any question at all no matter how simple.
0 x



aka Tin
Image gimplearn.net                                              

User avatar
RJKD
Tutorial Creator
View: Showcase | Slideshow
Topic Posts: 3/18
Posts: 1353
Joined: 03 Oct 2016, 13:01
Cash on hand: 10,349.49 GLP
Bank: 0.00 GLP
Gender: Guy
Location: Sheffield UK
Operating System: Windows
GIMP Version: 2.8.20
x 1490
x 210
Contact:
Great Britain

Post#4 by RJKD » 11 Oct 2017, 15:55


Your video demo was really easy to follow, how about posting a link here?
0 x




User avatar
tim

Administrator
View: Showcase | Slideshow
Topic Posts: 10/18
Posts: 2162
Joined: 26 Sep 2016, 00:16
Cash on hand: 7,946.37 GLP
Bank: 51,356.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 1207
x 258
Contact:
Canada

Post#5 by tim » 11 Oct 2017, 15:57


posting a link from where to where? from the video to this thread?

PS: I just added link to this Text tutorial from video and included video at end of this tutorial post
0 x



aka Tin
Image gimplearn.net                                              

User avatar
RJKD
Tutorial Creator
View: Showcase | Slideshow
Topic Posts: 3/18
Posts: 1353
Joined: 03 Oct 2016, 13:01
Cash on hand: 10,349.49 GLP
Bank: 0.00 GLP
Gender: Guy
Location: Sheffield UK
Operating System: Windows
GIMP Version: 2.8.20
x 1490
x 210
Contact:
Great Britain

Post#6 by RJKD » 11 Oct 2017, 15:59


Yes, that's my suggestion?
0 x




User avatar
tim

Administrator
View: Showcase | Slideshow
Topic Posts: 10/18
Posts: 2162
Joined: 26 Sep 2016, 00:16
Cash on hand: 7,946.37 GLP
Bank: 51,356.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 1207
x 258
Contact:
Canada

Post#7 by tim » 11 Oct 2017, 16:01


Good suggestion.
I just added link to this text tutorial from video and included video in the end of 1st post.
1 x



aka Tin
Image gimplearn.net                                              

Post Reply

Last 20 Members Who Visited This Topic

dfkettle (27), User avatar RJKD (21), taylor21 (1), User avatar leopardo-40 (1), User avatar gnerd (3), User avatar tim (49), User avatar animicule (4), User avatar Pat625 (4)

  • Similar Topics
    Replies
    Views
    Last post
• Featuring Members' Works (Image Attachments Only) •
1: Wallace
2: leopardo-40
3: RJKD
4: oldmangrumpy
5: Issabella
6: RJKD
7: Pat625

Who is online

Users browsing this forum: No registered users and 1 guest