It is currently Mon Aug 17, 2026 12:50 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Is Gimp the right tool?
PostPosted: Sat Sep 08, 2018 8:34 pm  (#1) 
Offline
New Member

Joined: Sep 08, 2018
Posts: 4
I'm trying to automate some simple tasks that are normally done by humans using Photoshop.

As a long-time FOSS evangelist, Gimp was my first thought. After a couple days of playing about with the Python console and plugins written by other people, I'm beginning to wonder if I should be looking to OpenCV or something else.

Lets imagine a somewhat oversimplified example.

We have an image that has two black shapes on a white background. I want to measure the distance between the shapes and make a red X if they the distance between the two objects isn't at lest 50 pixels.

Is that something I could do in the Gimp/Python microcosm?

If I want to do this in batches, is learning Gimp the right place for me to direct my efforts?

Thanks!


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: Is Gimp the right tool?
PostPosted: Sat Sep 08, 2018 9:28 pm  (#2) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
drawing the red X is the simple part,
...determining whether the two black shapes are is a bit more challenging.. I am trying to think of how a user might determine location of these black shapes.
Googling for how to determine distance between two shapes didn't show me an promising results.
I think that's going to be challenging no matter what if you use Photoshop or GIMP. If you want to automate it's going to be challenging at least to me.
It is a simple task done by humans, but to automate it you have to think like if you were to turn it into logics, how would you mathematically describe it.
I am interested in your findings.

_________________
TinT


Top
 Post subject: Re: Is Gimp the right tool?
PostPosted: Sat Sep 08, 2018 10:10 pm  (#3) 
Offline
New Member

Joined: Sep 08, 2018
Posts: 4
Right.

I mean, if you look at the complexity of 'gimp_image_select_contiguous_color' then obviously it's well within scope of what Gimp could do under the hood.

What I've not yet figured out is how much of the image's data is available to me while within Python. If I have access to complete image data I could do something like:
1. find leftmost black pixel location
* lets call this object A
2. select_contiguous_color
3. find rightmost pixel of object A

4. skip white pixels until we find another black
* this is object B
5. measure pixels between objects A/B (values from steps 3 and 4)

I'm just not sure if I'm extract those data points into variables with Gimp

I'll let you know if I figure it out.

Thanks!


Top
 Post subject: Re: Is Gimp the right tool?
PostPosted: Sat Sep 08, 2018 10:17 pm  (#4) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
This is like what I would do since I think GIMP's plugin is my hammer and everything else is a nail :D
And since I am not great at efficiency here's my hackish way of attacking the problem.
First I would find a point in polygon function on the internet there are many resources that have them (in python).

1. Find the 2 shapes by:
- Loop through all the pixels (maybe not every pixel if shapes are always minimum a certain size, then maybe jump every 10 pixels). Find a black pixel.
- Use fuzzy select tool to select at that black pixel to select that shape. Turn it into path. call this path1. Turn path1 into array of points (rough estimates at equal distance around the path). Call this array poly1.
- do this for rest of pixels looking for the next black pixel that isn't (point in polygon of poly1).
- Use fuzzy select tool to select that 2nd pixel. Turn it into path. call this path2. Turn path2 into array of points (rough estimates at equal distance around the path). Call this array poly2.
2 - Decide that the two polygons aren't colliding by:
- for every point in poly1 check and make sure they aren't (point in poly2).
3 - Determine shortest distance between two shapes by:
- for every point in poly1, check against every point in poly2 to find minimum distance. this minimum distance is the shortest distance between the two shapes, while checking record where these two points (one point in poly1, and one point in poly2) that have the minimum distance.
4 - If the recorded 2 points are less than 50, draw your red X.

Probably not the most efficient, but all these steps are doing in GIMP.

_________________
TinT


Top
 Post subject: Re: Is Gimp the right tool?
PostPosted: Sat Sep 08, 2018 10:19 pm  (#5) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
maybe ofnuts will chime in with a way more efficient method. :D

_________________
TinT


Top
 Post subject: Re: Is Gimp the right tool?
PostPosted: Sat Sep 08, 2018 10:24 pm  (#6) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
left most and right most?
Are your shapes always on the left and right of each other?
That simplifies things down alot.
So instead of checking to see if points in poly1 are in poly2, you can just look for maximum(x) and minimum(x)'s ..well you know what I mean.
I thought the shapes could be anywhere like one on top, on bottom or other orientation as well.

_________________
TinT


Top
 Post subject: Re: Is Gimp the right tool?
PostPosted: Sat Sep 08, 2018 10:46 pm  (#7) 
Offline
New Member

Joined: Sep 08, 2018
Posts: 4
Left and right is fine. I was just trying to think of the simplest example I could offer that would get me over the hump of the immediate obstruction. Sorry my example wasn't more clear; was making it up as I went along.

All of the plugins that I've downloaded so far mostly apply a succession of filters.

But after reading your reply, I found 'gimp_drawable_get_pixel' which helps. Now, I understand how I can be reactive to the content in the image and have something to play with for a while.

Thanks so much!


Top
 Post subject: Re: Is Gimp the right tool?
PostPosted: Sat Sep 08, 2018 11:12 pm  (#8) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
This list might give you more to work with.

pdb.gimp_drawable_get_pixel (as you mentioned)
pdb.gimp_image_select_contiguous-color (was suggested as gimp-fuzzy-select is deprecated)
pdb.plug-in-sel2path (this one was always tricky to search for because of the number 2)
pdb.gimp_image_get_active_vectors (might not need this if you can just look at image.vectors
image.vectors will return a list of vectors
then with each vector you can say vector.strokes returns a list of strokes
then stroke.get_length(precision) will return the length of that stroke just use 1 for
precision if you want to the pixel I think.
then you can use stroke.get_point_at_dist(dist,precision) which will return x,y,slope,valid
you'll probably only need x,y

There are similar names but longer for pdb.* calls for stroke and get length and and get_point_at_dist...but whenever i can use it directly by looking through the objects I will try to do it (I think it's faster than going through the pdb).
The rest i think you're able to search in the procedure browser...

Have fun. I look forward to your outcome.

_________________
TinT


Top
 Post subject: Re: Is Gimp the right tool?
PostPosted: Sat Sep 08, 2018 11:37 pm  (#9) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
if you're just always gonna have simple shape on right/left of each other
and only care about left/right most, you could probably get away with
pdb.gimp_selection_bounds instead of looking all all polygon points of shape

_________________
TinT


Top
 Post subject: Re: Is Gimp the right tool?
PostPosted: Sun Sep 09, 2018 6:12 am  (#10) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4812
What I would do:

1) select a color (gimp-image-select-color())
2) transform at that selection into a path (plug-in-sel2path())
3) the path will have at least two "strokes" (close sequence of Bézier splines)
4) Using their anchors:
- you can compute an approximation of their area (area of the polygon of the anchors), and to filter out the small ones to keep only the two big ones
- you can compute their distance (smallest distance between two anchors/tangents) or distance between the centroids.

This said OpenCV and pillow are likely a better way.

_________________
Image


Top
 Post subject: Re: Is Gimp the right tool?
PostPosted: Sun Sep 09, 2018 8:16 am  (#11) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
I have never used anchors or heard of anchors before. But it does sound more efficient especially the part about just starting of with looking for color select and get paths instead of pixel search.

_________________
TinT


Top
 Post subject: Re: Is Gimp the right tool?
PostPosted: Sun Sep 09, 2018 2:00 pm  (#12) 
Offline
GimpChat Member
User avatar

Joined: Dec 26, 2014
Posts: 205
An interesting problem

If you are needing to know the exact pixel difference between the black shapes the script in the zip file won't help, but it does determine if the distance between the two objects isn't at least 50 pixels.

Attachment:
two-black-shapes.zip [7.57 KiB]
Downloaded 70 times

You need the error console open to see the result

The script works on my GIMP 2.8.22 (an occasional fail, not sure why yet)

I tested it on the two pngs in the zip file

The script can be found at <image>\On test\two black shapes, and it works only on RGB images

steve


Top
 Post subject: Re: Is Gimp the right tool?
PostPosted: Mon Sep 10, 2018 7:54 am  (#13) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4812
trandoductin wrote:
I have never used anchors or heard of anchors before. But it does sound more efficient especially the part about just starting of with looking for color select and get paths instead of pixel search.

"Anchors" is just another name for the control points at both ends of a Bézier curve.

_________________
Image


Top
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 5 hours [ DST ]



* Login  



Powered by phpBB3 © phpBB Group