It is currently Mon Jul 01, 2024 3:13 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: My first Python Script for Gimp
PostPosted: Sun Apr 26, 2020 1:56 pm  (#1) 
Offline
GimpChat Member
User avatar

Joined: Apr 18, 2020
Posts: 7
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
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: My first Python Script for Gimp
PostPosted: Sun Apr 26, 2020 3:21 pm  (#2) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4752
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

_________________
Image


Top
 Post subject: Re: My first Python Script for Gimp
PostPosted: Sun Apr 26, 2020 8:44 pm  (#3) 
Offline
Global Moderator
User avatar

Joined: Oct 06, 2010
Posts: 4050
ofnuts wrote:
Welcome to the club :jumpclap


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

_________________
"In order to attain the impossible, one must attempt the absurd."
~ Miguel de Cervantes


Top
 Post subject: Re: My first Python Script for Gimp
PostPosted: Mon Apr 27, 2020 2:54 am  (#4) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4752
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

_________________
Image


Top
 Post subject: Re: My first Python Script for Gimp
PostPosted: Mon Apr 27, 2020 1:32 pm  (#5) 
Offline
GimpChat Member
User avatar

Joined: Apr 18, 2020
Posts: 7
Where can I find an updated list of parameters for the register function.


Top
 Post subject: Re: My first Python Script for Gimp
PostPosted: Mon Apr 27, 2020 2:56 pm  (#6) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4752
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.

_________________
Image


Top
 Post subject: Re: My first Python Script for Gimp
PostPosted: Tue Apr 28, 2020 2:33 am  (#7) 
Offline
GimpChat Member
User avatar

Joined: Sep 27, 2016
Posts: 345
Location: Germany, BW
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

_________________
Regards
nelo

(Gimp 2.10 on Linux Mint MATE 20.1)


Top
 Post subject: Re: My first Python Script for Gimp
PostPosted: Tue Apr 28, 2020 8:08 am  (#8) 
Offline
GimpChat Member
User avatar

Joined: Aug 08, 2016
Posts: 2060
Location: East Midlands of England
I think you have done very well with your scripting. Keep going. I really struggle with it. Python for me every time.

_________________
Image

"Let no one steal your dreams."
Paul Cookson


Latest plug-in update: Paragrapher v.1.4
Custom Font Links
Tools
Character Paths
White Bases


Top
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) Learning to do first Python script

6

No new posts .py script not showing under Python-Fu

3

No new posts Does anyone have script fu and python plugins that work on 2.99.19?

1

No new posts Can a python script use gimpfu, run from terminal, and create new img?

2

No new posts Eager to learn to input text in python script (Solved)

2



* Login  



Powered by phpBB3 © phpBB Group