It is currently 19 Feb 2018, 19:45

GIMP/GL QuestionsQuestion about gimp_vectors_stroke_new_from_points() (Showcase | Slideshow)

GIMP or GIMP LEARN forum related Questions
Post Reply
No avatar
dfkettle

Forum Member
View: Showcase | Slideshow
Topic Posts: 4/7
Posts: 9
Joined: 13 Jan 2018, 20:17
Cash on hand: 195.99 GLP
Gender: Guy
Location: Toronto, ON, Canada
Website: http://www.cuartooscuro.ca
Operating System: Windows
GIMP Version: 2.8.22
x 1
Canada

Question about gimp_vectors_stroke_new_from_points()

Post#1 by dfkettle » 1 week 2 days ago


I'm trying to do something which I think should be very simple, namely draw a single line segment in a script. There are probably several ways to do this, but this is what I came up with:

1. Create an empty Vector (is that the right term?) with gimp_vectors_new().
2. Add it to the image using gimp_image_insert_vectors().
3. Fill it with two points (four values) using gimp_vectors_stroke_new_from_points().
4. Select it with gimp_vectors_to_selection() followed by gimp_selection_border().
5. Colour it with gimp_edit_bucket_fill().

To see if this would work, I tried entering the steps manually in the Python console, but only got as far as step 3 before I got an error I couldn't understand. And in step 2, I had to use gimp_image_add_vectors(), even though it's deprecated according to the documentation, because I got a TypeError using gimp_image_insert_vectors().

Here's a transcript of my console session. Can anyone explain what's causing the errors in steps 2 and 3? Or is there an easier way to draw a single line?

Code: Select all

>>> image = pdb.gimp_image_new(800,800,RGB)
>>> layer = pdb.gimp_layer_new(image,image.width,image.height,RGBA_IMAGE,"Vector",100.0,NORMAL_MODE)
>>> pdb.gimp_image_insert_layer(image,layer,None,0)
>>> points = []
>>> points.append(0)
>>> points.append(0)
>>> points.append(400)
>>> points.append(400)
>>> v = pdb.gimp_vectors_new(image,"vectors")
>>> pdb.gimp_image_insert_vectors(image,v,0,-1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: wrong parameter type
>>> pdb.gimp_image_add_vectors(image,v,0)
>>> sid = pdb.gimp_vectors_stroke_new_from_points(v,VECTORS_STROKE_TYPE_BEZIER,len(points),points,FALSE)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
RuntimeError: execution error
>>> 
0 x




Registered members enjoy ads-free content
User avatar
tim
Administrator
View: Showcase | Slideshow
Topic Posts: 3/7
Posts: 2157
Joined: 26 Sep 2016, 00:16
Cash on hand: 7,894.01 GLP
Bank: 51,356.25 GLP
Gender: Guy
Location: Victoria, BC
Website: https://gimplearn.net
Operating System: Windows
GIMP Version: 2.8.22
My Personal Blog
My FineArtAmerica
x 1196
x 256
Contact:
Canada

Post#2 by tim » 1 week 2 days ago


1. If there is no parent, you must specify None like this: pdb.gimp_image_insert_vectors(image,v,None,-1)
2. gimp-vectors-stroke-new-from-points takes a list of control points. Each point actually has a left control point, the actual point, and a right control point. If you want to make them act like points then just make the left and right control points the same as the actual point. So to specify a line that starts at one point and ends in another, you need total of 6 control points (3 for each point).

Code: Select all

	image = pdb.gimp_image_new(800,800,RGB)
	pdb.gimp_display_new(image)
	layer = pdb.gimp_layer_new(image,image.width,image.height,RGBA_IMAGE,"Vector",100.0,NORMAL_MODE)
	point1 = [0,0] * 3
	point2 = [400,400] * 3
	points = point1 + point2
	v = pdb.gimp_vectors_new(image,"vectors")
	#pdb.gimp_image_insert_vectors(image,v,0,-1)
	pdb.gimp_image_insert_vectors(image,v,None,-1)
	sid = pdb.gimp_vectors_stroke_new_from_points(v,VECTORS_STROKE_TYPE_BEZIER,len(points),points,False)
In addition to that, what you're trying to do won't exactly work because in order to select border you need to have a shape selected.
When you do path to selection of a line it doesn't select anything so you have no shape to apply border selection to.
What you can do is, stroke the path with something like a pixel wide on a new transparent layer, then alpha to selection that layer to select the thin line (now it has a shape) then do border selection and that will work.
0 x



aka Tin
Image gimplearn.net                                              

No avatar
dfkettle

Forum Member
View: Showcase | Slideshow
Topic Posts: 4/7
Posts: 9
Joined: 13 Jan 2018, 20:17
Cash on hand: 195.99 GLP
Gender: Guy
Location: Toronto, ON, Canada
Website: http://www.cuartooscuro.ca
Operating System: Windows
GIMP Version: 2.8.22
x 1
Canada

Post#3 by dfkettle » 1 week 2 days ago


Thanks again. I was going by the documentation in the console browser, which tells you to set the parent argument to 0, not None. Should I report this somewhere as a mistake in the documentation?

I haven't tried your solution yet, I will later today. But this all seems like a lot of work just to draw a single straight line segment. In the past, I've done some work in Python using PIL (Python Imaging Library). I don't know if you're familiar with it, but drawing vector shapes like ellipses, polygons, points and lines are just simple calls to a single method. Unfortunately, I don't know if it's possible to install the PIL library within GIMP. And since the API is different, I'm not sure how to integrate the two anyway. The objects provided by GIMPFU and PIL are completely different. Do you know if anyone has tried?
0 x




User avatar
tim
Administrator
View: Showcase | Slideshow
Topic Posts: 3/7
Posts: 2157
Joined: 26 Sep 2016, 00:16
Cash on hand: 7,894.01 GLP
Bank: 51,356.25 GLP
Gender: Guy
Location: Victoria, BC
Website: https://gimplearn.net
Operating System: Windows
GIMP Version: 2.8.22
My Personal Blog
My FineArtAmerica
x 1196
x 256
Contact:
Canada

Post#4 by tim » 1 week 2 days ago


0 works if it was done in Scheme-Fu, in Python it's None. I don't know if you should report it or not as all other cases in Python it also uses None like for example when inserting a new layer, and no Parent, then I also use None.

If you draw a straight line using paintbrush or pencils then you only need 2 points as well (not 6).

But you're working vectors, where the path is considered a BEZIER curve so they have extra handle points so that you can have a curved line if you wanted.

I vaguely know about PIL, i think if you use PIL then you're drawing in environment completely different from GIMP.
I guess you could draw in PIL save that as image and then open it in GIMP but while in GIMP, if you want to manipulate things you'll have to use their procedures (found in procedure browser).
0 x



aka Tin
Image gimplearn.net                                              

No avatar
dfkettle

Forum Member
View: Showcase | Slideshow
Topic Posts: 4/7
Posts: 9
Joined: 13 Jan 2018, 20:17
Cash on hand: 195.99 GLP
Gender: Guy
Location: Toronto, ON, Canada
Website: http://www.cuartooscuro.ca
Operating System: Windows
GIMP Version: 2.8.22
x 1
Canada

Post#5 by dfkettle » 1 week 2 days ago


That's what I've done in the past, created something in Python/PIL then loaded it into Gimp or some other photo-editing software for further editing. I was hoping to skip the extra step of saving and loading. Some things seem easier to do with PIL, and other things seem easier in Python-Fu.
0 x




User avatar
tim
Administrator
View: Showcase | Slideshow
Topic Posts: 3/7
Posts: 2157
Joined: 26 Sep 2016, 00:16
Cash on hand: 7,894.01 GLP
Bank: 51,356.25 GLP
Gender: Guy
Location: Victoria, BC
Website: https://gimplearn.net
Operating System: Windows
GIMP Version: 2.8.22
My Personal Blog
My FineArtAmerica
x 1196
x 256
Contact:
Canada

Post#6 by tim » 1 week 1 day ago


If you border select a path and then fill it with a color.
GIMP's border has feathering, I am not sure how to get rid of feathering. But you might as well just set brush size to border width and then use the pencil tool to draw a line.
Something like this:

Code: Select all

#!/usr/bin/env python

# test.py rel 1
# test script
# # Created by Tin Tran http://gimplearn.net
# Comments directed to http://gimplearn.com or http://gimp-forum.net or http://gimpchat.com or http://gimpscripts.com
#
# License: GPLv3
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# To view a copy of the GNU General Public License
# visit: http://www.gnu.org/licenses/gpl.html
#
#
# ------------
#| Change Log |
# ------------
# Rel 1: Initial release. 
#import string
#import Image
from gimpfu import *
#from array import array
#import sys
def python_tt_test(image, layer, border_width, color):
	pdb.gimp_image_undo_group_start(image)
	pdb.gimp_context_push()
	
	#create new image and new layer
	new_image = pdb.gimp_image_new(800,800,RGB)
	pdb.gimp_display_new(new_image)
	new_layer = pdb.gimp_layer_new(new_image,new_image.width,new_image.height,RGBA_IMAGE,"my layer 1",100,NORMAL_MODE)
	pdb.gimp_image_insert_layer(new_image,new_layer,None,0)
	
	#brush related settings
	pdb.gimp_context_set_brush("2. Hardness 100")
	pdb.gimp_context_set_brush_size(border_width)
	pdb.gimp_context_set_opacity(100)
	
	#pencil tool to draw a thin line using above context brush settings
	point1 = [0,0]
	point2 = [400,400]
	points = point1 + point2
	pdb.gimp_context_set_foreground(color)
	pdb.gimp_pencil(new_layer,4,points)
	
	
	
	#we're done we'll do a selection none
	#pdb.gimp_selection_none(new_image)

	
	pdb.gimp_image_undo_group_end(image)
	pdb.gimp_displays_flush()
    #return

register(
	"python_fu_tt_test",                           
	"Test",
	"Test",
	"Tin Tran",
	"Tin Tran",
	"Feb 2018",
	"<Image>/Python-Fu/A test...",             #Menu path
	"RGB*, GRAY*", 
	[
	(PF_SPINNER, "border_width", "Border Width (px):", 20, (1, 1000, 1.0)),
	(PF_COLOR, "color", "Color:", (255, 0, 0)),
	#(PF_SPINNER, "dimension", "Tile Dimension(pixels):", 50, (10, 500, 1)),
	#(PF_SPINNER, "blur_radius", "Guasian Blur Radius:", 33, (0, 12800, 0.1)),  # alias PF_ADJUSTMENT
	],
	[],
	python_tt_test)

main()
0 x



aka Tin
Image gimplearn.net                                              

No avatar
dfkettle

Forum Member
View: Showcase | Slideshow
Topic Posts: 4/7
Posts: 9
Joined: 13 Jan 2018, 20:17
Cash on hand: 195.99 GLP
Gender: Guy
Location: Toronto, ON, Canada
Website: http://www.cuartooscuro.ca
Operating System: Windows
GIMP Version: 2.8.22
x 1
Canada

Post#7 by dfkettle » 1 week 1 day ago


Thanks, that works!
0 x




Post Reply

Last 20 Members Who Visited This Topic

User avatar leopardo-40 (1), dfkettle (14), User avatar tim (18), User avatar Conbagui (1), User avatar Wallace (1), User avatar Pat625 (2), User avatar RJKD (1)

  • Similar Topics
    Replies
    Views
    Last post
• Featuring Members' Works (Image Attachments Only) •
1: dinasset
2: oldmangrumpy
3: Anonymous
4: oldmangrumpy
5: oldmangrumpy
6: tim
7: dinasset

Who is online

Users browsing this forum: No registered users and 1 guest