It is currently Sat Jun 29, 2024 11:45 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: GIMP Python-Fu Plug-in template
PostPosted: Fri Nov 10, 2023 7:48 am  (#1) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3999
Location: Canada
This is here so that I can share what I do to write gimp plug-ins (in a future Vietnamese video as there seem to be not too many resources in Viet).
The template is in English of course.
It's meant to be used as a starting point for people who want to venture into writing their own python plugins for GIMP.

Just unzip to get the python-fu-template.py file, and start changing details and coding right away.

Or just copy the code below and paste it in your favorite text editor and start coding right away (old method):
#!/usr/bin/env python
# python-fu-template.py
# Created by Authorname
# This template was to be shared to http://gimpchat.com/viewtopic.php?f=12&t=20621
# 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 *
import random


def python_test(image,layer):
   pdb.gimp_image_undo_group_start(image)
   # pdb.gimp_context_push()
   #YOUR CODE BEGINS=======================
   pass
   pdb.gimp_message("This is just a blank template that does nothing right now.")
   #YOUR CODE ENDS ========================
   # pdb.gimp_context_pop()
   pdb.gimp_image_undo_group_end(image)
   pdb.gimp_displays_flush()

register(
   "python_fu_bibo_text",
   "Python-Fu Template",
   "Python-Fu Template",
   "Authorname",
   "Authorname",
   "November 10, 2023",
   "<Image>/Python-Fu/Test/Python-Fu-Template...",
   "*",
   [
   #INPUT BEGINS ======================
   #INPUT ENDS ========================
   ],
   [],
   python_test)

main()

# Below is all the example input types for INPUTS for the plug-in which can be cut and pasted into #INPUT BEGINS section and edited to taste
#           (PF_INT, "p0", "_INT:", 0), # PF_INT8, PF_INT16, PF_INT32  similar but no difference in Python.
#           (PF_FLOAT, "p02", "_FLOAT:", 3.141),
#           (PF_STRING, "p03", "_STRING:", "foo"),  # alias PF_VALUE
#           (PF_TEXT, "p04", "TEXT:", "bar"),
#           # PF_VALUE
#           # Pick one from set of choices
#           (PF_OPTION,"p1",   "OPTION:", 0, ["0th","1st","2nd"]), # initially 0th is choice
#           (PF_RADIO, "p16", "RADIO:", 0, (("0th", 1),("1st",0))), # note bool indicates initial setting of buttons
#           # PF_RADIO is usually called a radio button group.
#           # SLIDER, ADJUSTMENT types require the extra parameter of the form (min, max, step).
#           (PF_TOGGLE, "p2",   "TOGGLE:", 1), # initially True, checked.  Alias PF_BOOL
#           # PF_TOGGLE is usually called a checkbox.
#           (PF_SLIDER, "p3", "SLIDER:", 0, (0, 100, 10)),
#           (PF_SPINNER, "p4", "SPINNER:", 21, (1, 1000, 50)),  # alias PF_ADJUSTMENT
#           # Pickers ie combo boxes ie choosers from lists of existing Gimp objects
#           (PF_COLOR, "p14", "_COLOR:", (100, 21, 40) ), # extra param is RGB triple
#           # PF_COLOUR is an alias by aussie PyGimp author lol
#           (PF_IMAGE, "p15", "IMAGE:", None), # should be type gimp.image, but None works
#           (PF_FONT, "p17", "FONT:", 0),
#           (PF_FILE, "p18", "FILE:", 0),
#           (PF_BRUSH, "p19", "BRUSH:", 0),
#           (PF_PATTERN, "p20", "PATTERN:", 0),
#           (PF_GRADIENT, "p21", "GRADIENT:", 0),
#           (PF_PALETTE, "p22", "PALETTE:", 0),
#           (PF_LAYER, "p23", "LAYER:", None),
#           (PF_CHANNEL, "p24", "CHANNEL:", None),  # ??? Usually empty, I don't know why.
#           (PF_DRAWABLE, "p25", "DRAWABLE:", None),
#           # Mostly undocumented, but work
#           (PF_VECTORS, "p26", "VECTORS:", None),
#           (PF_FILENAME, "p27", "FILENAME:", 0),
#           (PF_DIRNAME, "p28", "DIRNAME:", 0)
#           # PF_REGION might work but probably of little use.  See gimpfu.py.

New method:
#!/usr/bin/env python
# python-fu-template.py
# Created by Authorname
# This template was to be shared to http://gimpchat.com/viewtopic.php?f=12&t=20621
# 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 *
import random


def python_test(image,drawable):
   pdb.gimp_image_undo_group_start(image)
   # pdb.gimp_context_push()
   #YOUR CODE BEGINS=======================
   pass
   pdb.gimp_message("This is just a blank template that does nothing right now.")
   #YOUR CODE ENDS ========================
   # pdb.gimp_context_pop()
   pdb.gimp_image_undo_group_end(image)
   pdb.gimp_displays_flush()

register(
   "python_fu_python_test", #register under pdb.
   "Python Test Tooltip", #Tooltip
   "Python Test Additional Info", #Additional Info
   "Author Name", #Author Name
   "GimpChat: http://gimpchat.com/viewtopic.php?f=12&t=20621",
   "2023.11.11",
   "Python-Fu-Template...", #menu item
   "RGB*",
   [
   #INPUT BEGINS
   (PF_IMAGE,      "image",       "Input image", None),
   (PF_DRAWABLE,   "drawable", "Input drawable", None),
   #INPUT ENDS
   ],
   [],
   python_test,
   menu="<Image>/Python-Fu/Test" #menu path will be under Python-Fu
   )

main()

# Below is all the example input types for INPUTS for the plug-in which can be cut and pasted into #INPUT BEGINS section and edited to taste
#           (PF_INT, "p0", "_INT:", 0), # PF_INT8, PF_INT16, PF_INT32  similar but no difference in Python.
#           (PF_FLOAT, "p02", "_FLOAT:", 3.141),
#           (PF_STRING, "p03", "_STRING:", "foo"),  # alias PF_VALUE
#           (PF_TEXT, "p04", "TEXT:", "bar"),
#           # PF_VALUE
#           # Pick one from set of choices
#           (PF_OPTION,"p1",   "OPTION:", 0, ["0th","1st","2nd"]), # initially 0th is choice
#           (PF_RADIO, "p16", "RADIO:", 0, (("0th", 1),("1st",0))), # note bool indicates initial setting of buttons
#           # PF_RADIO is usually called a radio button group.
#           # SLIDER, ADJUSTMENT types require the extra parameter of the form (min, max, step).
#           (PF_TOGGLE, "p2",   "TOGGLE:", 1), # initially True, checked.  Alias PF_BOOL
#           # PF_TOGGLE is usually called a checkbox.
#           (PF_SLIDER, "p3", "SLIDER:", 0, (0, 100, 10)),
#           (PF_SPINNER, "p4", "SPINNER:", 21, (1, 1000, 50)),  # alias PF_ADJUSTMENT
#           # Pickers ie combo boxes ie choosers from lists of existing Gimp objects
#           (PF_COLOR, "p14", "_COLOR:", (100, 21, 40) ), # extra param is RGB triple
#           # PF_COLOUR is an alias by aussie PyGimp author lol
#           (PF_IMAGE, "p15", "IMAGE:", None), # should be type gimp.image, but None works
#           (PF_FONT, "p17", "FONT:", 0),
#           (PF_FILE, "p18", "FILE:", 0),
#           (PF_BRUSH, "p19", "BRUSH:", 0),
#           (PF_PATTERN, "p20", "PATTERN:", 0),
#           (PF_GRADIENT, "p21", "GRADIENT:", 0),
#           (PF_PALETTE, "p22", "PALETTE:", 0),
#           (PF_LAYER, "p23", "LAYER:", None),
#           (PF_CHANNEL, "p24", "CHANNEL:", None),  # ??? Usually empty, I don't know why.
#           (PF_DRAWABLE, "p25", "DRAWABLE:", None),
#           # Mostly undocumented, but work
#           (PF_VECTORS, "p26", "VECTORS:", None),
#           (PF_FILENAME, "p27", "FILENAME:", 0),
#           (PF_DIRNAME, "p28", "DIRNAME:", 0)
#           # PF_REGION might work but probably of little use.  See gimpfu.py.


Attachments:
File comment: python-fu-template.py (new method)
after ofnut's suggestions

python-fu-template (2).zip [1.9 KiB]
Downloaded 115 times
File comment: python-fu-template.py (old method)
python-fu-template.zip [1.79 KiB]
Downloaded 112 times

_________________
TinT


Last edited by trandoductin on Sat Nov 11, 2023 10:00 am, edited 1 time in total.
Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: GIMP Python-Fu Plug-in template
PostPosted: Sat Nov 11, 2023 12:05 am  (#2) 
Offline
GimpChat Member
User avatar

Joined: May 24, 2021
Posts: 787
Location: SEA - South East Asia
(PF_COLOR, "p14", "_COLOR:", (100, 21, 40) ),
# PF_COLOUR is an alias by aussie PyGimp author lol

:hehe

I'm laughing, but I completely understand, all you life you write the British/Aussie way, which is "automatic" and when you write some code, you get slowed down just because of the spelling, this can be upsetting/painful especially when it bugs.
Nice found Tin and thanks a lot for the template :bigthup :tyspin

_________________
Patrice


Top
 Post subject: Re: GIMP Python-Fu Plug-in template
PostPosted: Sat Nov 11, 2023 4:30 am  (#3) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4752
PixLab wrote:
(PF_COLOR, "p14", "_COLOR:", (100, 21, 40) ),
# PF_COLOUR is an alias by aussie PyGimp author lol

:hehe

I'm laughing, but I completely understand, all you life you write the British/Aussie way, which is "automatic" and when you write some code, you get slowed down just because of the spelling, this can be upsetting/painful especially when it bugs.
Nice found Tin and thanks a lot for the template :bigthup :tyspin


There is a programming language created by a Briton (Rexx) that has both "centre" and "center" functions.

_________________
Image


Top
 Post subject: Re: GIMP Python-Fu Plug-in template
PostPosted: Sat Nov 11, 2023 4:41 am  (#4) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4752
trandoductin wrote:
This is here so that I can share what I do to write gimp plug-ins (in a future Vietnamese video as there seem to be not too many resources in Viet).
The template is in English of course.
It's meant to be used as a starting point for people who want to venture into writing their own python plugins for GIMP.

Just unzip to get the python-fu-template.py file, and start changing details and coding right away.

Or just copy the code below and paste it in your favorite text editor and start coding right away:
#!/usr/bin/env python
# python-fu-template.py
# Created by Authorname
# This template was to be shared to http://gimpchat.com/viewtopic.php?f=12&t=20621
# 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 *
import random


def python_test(image,layer):
   pdb.gimp_image_undo_group_start(image)
   # pdb.gimp_context_push()
   #YOUR CODE BEGINS=======================
   pass
   pdb.gimp_message("This is just a blank template that does nothing right now.")
   #YOUR CODE ENDS ========================
   # pdb.gimp_context_pop()
   pdb.gimp_image_undo_group_end(image)
   pdb.gimp_displays_flush()

register(
   "python_fu_bibo_text",
   "Python-Fu Template",
   "Python-Fu Template",
   "Authorname",
   "Authorname",
   "November 10, 2023",
   "<Image>/Python-Fu/Test/Python-Fu-Template...",
   "*",
   [
   #INPUT BEGINS ======================
   #INPUT ENDS ========================
   ],
   [],
   python_test)

main()

# Below is all the example input types for INPUTS for the plug-in which can be cut and pasted into #INPUT BEGINS section and edited to taste
#           (PF_INT, "p0", "_INT:", 0), # PF_INT8, PF_INT16, PF_INT32  similar but no difference in Python.
#           (PF_FLOAT, "p02", "_FLOAT:", 3.141),
#           (PF_STRING, "p03", "_STRING:", "foo"),  # alias PF_VALUE
#           (PF_TEXT, "p04", "TEXT:", "bar"),
#           # PF_VALUE
#           # Pick one from set of choices
#           (PF_OPTION,"p1",   "OPTION:", 0, ["0th","1st","2nd"]), # initially 0th is choice
#           (PF_RADIO, "p16", "RADIO:", 0, (("0th", 1),("1st",0))), # note bool indicates initial setting of buttons
#           # PF_RADIO is usually called a radio button group.
#           # SLIDER, ADJUSTMENT types require the extra parameter of the form (min, max, step).
#           (PF_TOGGLE, "p2",   "TOGGLE:", 1), # initially True, checked.  Alias PF_BOOL
#           # PF_TOGGLE is usually called a checkbox.
#           (PF_SLIDER, "p3", "SLIDER:", 0, (0, 100, 10)),
#           (PF_SPINNER, "p4", "SPINNER:", 21, (1, 1000, 50)),  # alias PF_ADJUSTMENT
#           # Pickers ie combo boxes ie choosers from lists of existing Gimp objects
#           (PF_COLOR, "p14", "_COLOR:", (100, 21, 40) ), # extra param is RGB triple
#           # PF_COLOUR is an alias by aussie PyGimp author lol
#           (PF_IMAGE, "p15", "IMAGE:", None), # should be type gimp.image, but None works
#           (PF_FONT, "p17", "FONT:", 0),
#           (PF_FILE, "p18", "FILE:", 0),
#           (PF_BRUSH, "p19", "BRUSH:", 0),
#           (PF_PATTERN, "p20", "PATTERN:", 0),
#           (PF_GRADIENT, "p21", "GRADIENT:", 0),
#           (PF_PALETTE, "p22", "PALETTE:", 0),
#           (PF_LAYER, "p23", "LAYER:", None),
#           (PF_CHANNEL, "p24", "CHANNEL:", None),  # ??? Usually empty, I don't know why.
#           (PF_DRAWABLE, "p25", "DRAWABLE:", None),
#           # Mostly undocumented, but work
#           (PF_VECTORS, "p26", "VECTORS:", None),
#           (PF_FILENAME, "p27", "FILENAME:", 0),
#           (PF_DIRNAME, "p28", "DIRNAME:", 0)
#           # PF_REGION might work but probably of little use.  See gimpfu.py.


If I may:

- The "python-fu" prefix in the atom is unnecessary (will be added anyway)
- You are using the old form of registration. In the new form the menu entry is just the final item ("Python-Fu-Template...") and there is a positional "menu" for the rest. This is important because if you use the new registration the first parameters are no longer implicit (you need a PF_IMAGE and PF_DRAWABLE parameter if you work on a layer), but you get a better behavior if you want to replay the filter. Also, your python function receives exactly the number of registered parameters.
- The image type ("*") is important. Many filters won't work on color-indexed images and many will have problems with grayscale images. A better default is "RGB*"

_________________
Image


Top
 Post subject: Re: GIMP Python-Fu Plug-in template
PostPosted: Sat Nov 11, 2023 10:01 am  (#5) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3999
Location: Canada
Thanks ofnuts,
I added new version to first post.

_________________
TinT


Top
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts use in python of plug-in lighting

4

No new posts GIMP 2.10 doesn't install my python plug-ins

1

No new posts Plug-in crashes after OS upgrade: python version mismatch? maybe?

4

No new posts Attachment(s) Map Transform - using Template

10

No new posts Convert GIMP plugin from Python 2 to Python 3

4



* Login  



Powered by phpBB3 © phpBB Group