GIMP Chat
http://gimpchat.com/

My first Python Script for Gimp
http://gimpchat.com/viewtopic.php?f=9&t=18279
Page 1 of 1

Author:  mazzolav [ Sun Apr 26, 2020 1:56 pm ]
Post subject:  My first Python Script for Gimp

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

Attachments:
customguides.7z [1.27 KiB]
Downloaded 119 times

Author:  ofnuts [ Sun Apr 26, 2020 3:21 pm ]
Post subject:  Re: My first Python Script for Gimp

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:
#!/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)
    [
        (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:
    "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):
      (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:
    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.
    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:
    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

Author:  mahvin [ Sun Apr 26, 2020 8:44 pm ]
Post subject:  Re: My first Python Script for Gimp

ofnuts wrote:
Welcome to the club :jumpclap


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

Author:  ofnuts [ Mon Apr 27, 2020 2:54 am ]
Post subject:  Re: My first Python Script for Gimp

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

Author:  mazzolav [ Mon Apr 27, 2020 1:32 pm ]
Post subject:  Re: My first Python Script for Gimp

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

Author:  ofnuts [ Mon Apr 27, 2020 2:56 pm ]
Post subject:  Re: My first Python Script for Gimp

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.

Author:  nelo [ Tue Apr 28, 2020 2:33 am ]
Post subject:  Re: My first Python Script for Gimp

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

Author:  Skinnyhouse [ Tue Apr 28, 2020 8:08 am ]
Post subject:  Re: My first Python Script for Gimp

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

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/