Switch to full style
Post all Gimp scripts and script writing questions here
Post a reply

My first Python Script for Gimp

Sun Apr 26, 2020 1:56 pm

I created a simple script that will create guides that are evenly spaced both Horizontally, and Vertically.
You can choose the number of guides separately, that is you can 3 guides Horizontally and 4 guides Vertically.

Mazzolav

Re: My first Python Script for Gimp

Sun Apr 26, 2020 3:21 pm

Not bad, but:

Technical :

- Your code is missing a "Shebang" that identifies it as Python (for those folks on Linux and OSX). The first line should be:
Code:
#!/usr/bin/env python

- Your registration should have an explicit PF_IMAGE parameter (and then you drop the useless "drawable" parameter in the function definition)
Code:
    [
        (PF_IMAGE, 'image', 'Input image', None),
        (PF_INT, "int_Hor_Guide_Cnt",  "Number of Horizontal Sections", 0),
        (PF_INT, "int_Vert_Guide_Cnt", "Number of Verticle Sections",   0)
    ],

- You are using a deprecated menu registration method, current method has a specific named parameter to specify the menu. In addition the current trend is to avoid adding items in the already busy image menu bar. Put your code with the other guide-related menus:
Code:
    "April 2020",
    "Custom Guides",
    "*",
    [
        (PF_INT, "int_Hor_Guide_Cnt",  "Number of Horizontal Sections", 0),
        (PF_INT, "int_Vert_Guide_Cnt", "Number of Verticle Sections",   0)
    ],
    [],
    void_create_custom_guides,
   menu='<image>/Image/Guides/'

- Your description field is a bit misleading since the entered count applies to the number of sections not the number of guides
- You don't need the "from inspect import trace"
- You should bracket the function body between image.undo_group_start() and image.undo_group_end() so that the effect of your plugin can be undone with a single Ctr-Z (otherwise it is on Ctrl-Z per added guide).
- Your script crashes with en entry count of 0 (and does weird things with negative values).In practice your smaller entry is 1 (which also happens to be a valid value for which no guide is added), you can have the check done for you using a spinner (PF_SPINNER) instead of a free entry field (PF_INT):
Code:
      (PF_SPINNER, "int_Hor_Guide_Cnt",  "Number of Horizontal Sections",  1, (1,100, 1)),


Coding:

- :shoot Hungarian notation is really outmoded, and when used with snake_case is a complete eyesore (I won't even mention your attempts at a novel snake_And_Camel_Case). And if you do it, at least do it fully, you missed out a gimp_image_image and a gimp_drawable_drawable :evilgrin .
- Copying a variable into a variable when neither changes is pointless (int_image_width/int_image_height)
- You can easily avoid a pair of variables:
Code:
    int_hguide = int_horz_space
    for cnt in range( 1, int_Hor_Guide_Cnt ):
        pdb.gimp_image_add_hguide( image, int_hguide )
        int_hguide += int_horz_space

v.s.
Code:
    for cnt in range( 1, int_Hor_Guide_Cnt ):
        pdb.gimp_image_add_hguide( image, int_horz_space*cnt )

but in a more pythonic way you would just let Python iterate the positions:
Code:
    for guidePos in range( int_Hor_Guide_Cnt,image.width,int_Hor_Guide_Cnt ):
        pdb.gimp_image_add_hguide( image, guidePos)


Finally, it's all fine and dandy until the number of sections doesn't divide the image width/height, so you have to either not do it, or use some more complex code to try to spread the excess/missing pixels across some sections so that the end result still looks good.

Welcome to the club :jumpclap

Re: My first Python Script for Gimp

Sun Apr 26, 2020 8:44 pm

ofnuts wrote: Welcome to the club :jumpclap


My first Python script would have gotten me booted from the club. :rofl

Re: My first Python Script for Gimp

Mon Apr 27, 2020 2:54 am

mahvin wrote:
ofnuts wrote: Welcome to the club :jumpclap


My first Python script would have gotten me booted from the club. :rofl


"I refuse to join any club that would have me as a member." Groucho Marx

Re: My first Python Script for Gimp

Mon Apr 27, 2020 1:32 pm

Where can I find an updated list of parameters for the register function.

Re: My first Python Script for Gimp

Mon Apr 27, 2020 2:56 pm

mazzolav wrote:Where can I find an updated list of parameters for the register function.


https://gitlab.gnome.org/GNOME/gimp/-/t ... pygimp/doc

Check the HTML files, but a look at the source code (parent directory) can teach you things too.

Re: My first Python Script for Gimp

Tue Apr 28, 2020 2:33 am

mazzolav wrote:Where can I find an updated list of parameters for the register function.


Does this one on Akkana's site help?
https://gimpbook.com/scripting/gimp-scr ... es/pyui.py

Re: My first Python Script for Gimp

Tue Apr 28, 2020 8:08 am

I think you have done very well with your scripting. Keep going. I really struggle with it. Python for me every time.
Post a reply