Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Divide selection using guides
#1
Hello,

First time here Smile

Anyway, I thought it would be useful if I could tell GIMP to use current selection as area for setting guides.

There is an option called New guide from selection (Images -> Guides -> New guide from selection), but this just
puts 2 horizontal and 2 vertical guides along sides of selection.


What I had in mind was to use current rectangular selection (or bounding box of it) and then "divide" it into equal parts by
setting guides  (number settable by user), either horizontally, vertically, or both.

So for example, let's say I have some kind of object on my canvas, and it's already placed on the right spot. 
And maybe I want to do some work on the middle third. 

I could use measure tool, but this won't work without calculator.


Another option would be using rectangular selection and then setting Rule of thirds under tool options.
But choosing any tool after that results in "guides" vanishing inside of the selection, so you have to put guides
manually right after making that selection, if you don't want to loose them.

Also, if you need any number of divisions that's not already predefined (center, thirds, fifths...) this technique will not really work. 
It can be done, but it will definitely take lots of repetitive steps.

So I wrote a small plugin that does exactly what I had in mind.

It's not finished yet, but it works. 

So how do you use it? After placing it in plugin directory, it registers itself under
Image -> Guides -> Divide selection using guides

First you make a selection. It doesn't have to be rectangular, and if it's not it will use the bounding box of the selection
for calculation. After making a selection, choose Divide selection using guides (under Image -> Guides).
Enter the number of divisions you need, and that's it.

Now, maybe I'm re-discovering here something that's common knowledge among gimpers,
but even if that's the case, this was a good learning experience. The only problem is I'm
getting lots of GEGL related errors (GIMP 2.10.32), but from what I could read here, it's not my plugin's fault. Smile


Attached Files
.zip   g_divide_selection_using_guides.zip (Size: 725 bytes / Downloads: 130)
Reply
#2
I'm laughing right now, not about you, let me explain.

I start to read your post, and reaching something like 2/3 of it, I told myself "This guy/gal looks like s/he knows what s/he is speaking about, with all example the s/he gave, what's her/his problem?"
Then I came to the last 3rd part of your comment and understood you were proposing a plugin, that why I'm laughing > misunderstanding your post Wink

In all case thanks a lot for it I'll try it tomorrow (it's very late evening in my time zone), and I'm thinking already for some useful use, for sure your plugin will be useful.
Again thank you very much, please have a fantastic day/evening or night.
Reply
#3
(04-14-2023, 02:11 PM)PixLab Wrote: I'm laughing right now, not about you, let me explain.

I start to read your post, and reaching something like 2/3 of it, I told myself "This guy/gal looks like s/he knows what s/he is speaking about, with all example the s/he gave, what's her/his problem?"


Well one reason I went into details is to get some feedback - is there anything else out there, how other people deal with this...? It was like, if they don't use the measure tool in combination with calculator on their phone, what do they use? Smile

Another was to convince myself that I'm not missing anything important. And that is the hard part,
GIMP's UI is pretty complex, not always the most intuitive (well *of course* layer name in bold means there is no alpha channel Smile and on top of that -  AFAIK,  there is no single repository of useful scripts, so lots of useful stuff is probably scattered around.

So when I saw Ofnuts repository, I thought somebody with more experience must have already thought of this "divide selection with guides" problem.
Especially when I realized it wasn't really that hard to write (after initial hurdle) - at least this basic version.

So, I don't know - maybe I missed it, maybe there are other more effective solutions out there, maybe even in the GIMP itself, but I couldn't really find anything.

Don't expect too much, it's quite basic really. But I think it could be useful.
Reply
#4
(04-14-2023, 09:33 AM)goran Wrote: I could use measure tool, but this won't work without calculator.
Well maybe yes... In the input fields where Gimp expects a number, you can enter expressions. For instance, to scale a 4000px image to half, instead of computing 2000 on the side, you just enter 4000/2 in the size field (and since it already contains 4000, you just add /2).
Reply
#5
(04-14-2023, 04:46 PM)Ofnuts Wrote:
(04-14-2023, 09:33 AM)goran Wrote: I could use measure tool, but this won't work without calculator.
Well maybe yes... In the input fields where Gimp expects a number, you can enter expressions. For instance, to scale a 4000px image to half, instead of computing 2000 on the side, you just enter 4000/2 in the size field (and since it already contains 4000, you just add /2).

I never saw an input field on the measurement tool, OP and I are speaking about this tool > Measure

(04-14-2023, 04:20 PM)goran Wrote: Don't expect too much, it's quite basic really. But I think it could be useful.

I did tried today, nice and indeed it will be useful.

Two things though, would it be possible to change the PF_INT to something like PF_SPINNER ?
I took the freedom to slightly add 2 code lines to be able to do a Ctrl+Z at once and not have to do it multiple time (I'm no programmer, I hope it's ok)
Here:
Code:
#!/usr/bin/env python

#
# https://www.gimp-forum.net/Thread-Divide-selection-using-guides
#

from gimpfu import *
#img = gimp.image_list()[0]

def divide_selection_using_guides(img, x_div, y_div):
    pdb.gimp_image_undo_group_start(img)
    
    if x_div==0 or x_div=="":
        x_div=1
    if y_div==0 or y_div=="":
        y_div=1
        
    pdb.gimp_selection_bounds(img)
    selection_exists = pdb.gimp_selection_bounds(img)[0]
    x1=pdb.gimp_selection_bounds(img)[1]
    y1=pdb.gimp_selection_bounds(img)[2]
    x2=pdb.gimp_selection_bounds(img)[3]
    y2=pdb.gimp_selection_bounds(img)[4]
    selection_width = (x2-x1)*1.0
    selection_height = (y2-y1)*1.0
    
    x_offset = selection_width/x_div
    y_offset = selection_height/y_div
    
    #pdb.gimp_message(selection_width)
    #pdb.gimp_message(x_offset)
    
    
    for i in range(0, x_div+1):
        next_pos = x1 + i*x_offset
        pdb.gimp_image_add_vguide(img, next_pos)
    
    for i in range(0, y_div+1):
        next_pos = y1 + i*y_offset
        pdb.gimp_image_add_hguide(img, next_pos)
    
    pdb.gimp_image_undo_group_end(img)

    
register(
"g-divide-selection-using-guides",
"Creates guides inside of the selection",
"Divides selection using evenly spaced guides",
"Goran","Goran","2023",
"Divide selection using guides",
"*",
[
        (PF_IMAGE, "image","takes curr image", None),
        (PF_INT, "x_div", "horizontal divisions", 10),
    (PF_INT, "y_div", "vertical divisions", 10)


],
[],
divide_selection_using_guides, menu="<Image>/Image/Guides")


main()

Again thank you that's a nice script
Reply
#6
@goran,thanks to share the plugin. Nice work, the plugin is working on gimp 2.10.34 /windows 10.
Reply
#7
(04-15-2023, 04:02 AM)PixLab Wrote: I did tried today, nice and indeed it will be useful.

Two things though, would it be possible to change the PF_INT to something like PF_SPINNER ?

I took the freedom to slightly add 2 code lines to be able to do a Ctrl+Z at once and not have to do it multiple time (I'm no programmer, I hope it's ok)


Well I'm also new to this, but yes, from what I read PF_SPINNER requires additional argument.

it's a tuple in the form of (min, max, step). So if you change these two lines:
Code:
(PF_INT, "x_div", "horizontal divisions", 10),
(PF_INT, "y_div", "vertical divisions", 10)


with these:
Code:
(PF_SPINNER, "x_div", "horizontal divisions", 3, (1,10,1)),
(PF_SPINNER, "y_div", "vertical divisions", 3, (1,10,1))

You'll get a spinner that goes from 1 to 10 with the default value
of 3. If you need more, you can easily change number 10 in both lines.

But after testing it I realized it sends a float instead of an int,
so it messed up the range in the loop which requires int.

So you'll also have to cast the values, you can just put these two lines
in the beginning of the function (def line is the same as it was):

Code:
def divide_selection_using_guides(img, x_div, y_div):
   
   x_div = int(x_div)
   y_div = int(y_div)

That should be it.
Reply
#8
(04-15-2023, 07:50 AM)denzjos Wrote: @goran,thanks to share the plugin. Nice work, the plugin is working on gimp 2.10.34 /windows 10.

Good to know! Smile

And GEGL warnings, they are still there?
Reply
#9
Hi goran,

Thank you for sharing your first script. I appreciate getting into new things isn't easy so will just mention one thing.

In gimp if you do the menu item:
Filters -> Python-Fu -> Console

Then hit the Browse button

You can search for the function you want to call, so in your case gimp_selection_bounds.

When that comes up hit the apply button. Then look in the console window, and copy and paste the result into your code:

non_empty, x1, y1, x2, y2 = pdb.gimp_selection_bounds(image)

You can use the above one line to replace the following lines you currently have (changing non_empty and image to your preferred variable names) as there is no need for the multiple calls:

pdb.gimp_selection_bounds(img)
selection_exists = pdb.gimp_selection_bounds(img)[0]
x1=pdb.gimp_selection_bounds(img)[1]
y1=pdb.gimp_selection_bounds(img)[2]
x2=pdb.gimp_selection_bounds(img)[3]
y2=pdb.gimp_selection_bounds(img)[4]
Reply
#10
(04-15-2023, 11:01 AM)goran Wrote: So you'll also have to cast the values, you can just put these two lines
in the beginning of the function (def line is the same as it was):

Code:
def divide_selection_using_guides(img, x_div, y_div):
   
   x_div = int(x_div)
   y_div = int(y_div)

That should be it.

It's not working, the plugin don't load at all Wink
Reply


Forum Jump: