PIL - setting the width of lines in ImageDraw

M

Max M

I am using PIL to generate som graphs, like:

draw = ImageDraw.Draw(im)
for graph in self.graphs:
polygon = graph.getPolygon()
draw.polygon(polygon, outline=graph.color)
del draw

But the linewidth of my polygon is only one pixel wide. Is there any way
to set it to something else?

(How nice it would be to have css compatible parameters on polygons,
like: draw.polygon(polygon, border="1px solid gray"))

I have read the docs, and browsed the source. But me being a lowly
Windows developer I stopped at _imaging.py module, which the ImageDraw
module apparantly use to do the actual drawing.


regards Max M
 
L

Larry Bates

When you want lines wider than 1px you are actually
drawing rectangles with fillcolor set to the line
color. There is a drawrectangle method.

You should also take a look at www.reportlab.com
(the ReportLab Graphics module is great). I use
it to do almost all my graphing from Python.

Larry Bates
Syscon, Inc.
 
E

Erik Heneryd

Larry said:
When you want lines wider than 1px you are actually
drawing rectangles with fillcolor set to the line
color. There is a drawrectangle method.

Ever tried to draw a /slanted/ line?


Erik
 
L

Larry Bates

There's nothing wrong with "slanted" rectangles.
In PIL you just define upper left and lower right
coordinates of the rectangle in pixels. That's
what a thick line would do anyway.

Larry Bates
Syscon, Inc.
 
M

Max M

Larry said:
There's nothing wrong with "slanted" rectangles.
In PIL you just define upper left and lower right
coordinates of the rectangle in pixels. That's
what a thick line would do anyway.

Yeah I know I could do it that way. But I would rather avoid it. Which
is why I am using PIL to begin with.


regards Max M
 
E

Erik Heneryd

Larry said:
There's nothing wrong with "slanted" rectangles.
In PIL you just define upper left and lower right
coordinates of the rectangle in pixels. That's
what a thick line would do anyway.

Really? Didn't know that. Please show me how.


Erik
 
M

Max M

Erik said:
Really? Didn't know that. Please show me how.


The problem is that you have to calculate the rectangles corner
positions yourself. Which make it a bit less trivial than drawing lines.

If you want a fat line from ((0,0),(10,10)) you would need to calculate
where the corners should be yourself.

regards Max M
 
E

Erik Heneryd

Max said:
The problem is that you have to calculate the rectangles corner
positions yourself. Which make it a bit less trivial than drawing lines.

If you want a fat line from ((0,0),(10,10)) you would need to calculate
where the corners should be yourself.

Reason I was (sarcastically) asking about how was because Larry implied
it could be done easily using "drawrectangle", as opposed to calculating
the corners yourself and using polygon.

And ofcourse - just drawing thick lines isn't enough to do thick lined
polygons, you also have to deal with joining them.


Erik
 
J

John Hunter

Max> The problem is that you have to calculate the rectangles
Max> corner positions yourself. Which make it a bit less trivial
Max> than drawing lines.

Max> If you want a fat line from ((0,0),(10,10)) you would need to
Max> calculate where the corners should be yourself.

In matplotlib, you can draw antialiased lines with any color, style,
and width you want. And we'll do the calculations for you (TM)

# plot a green line, 4 points thick
plot(x, y, 'g', linewidth=4)

http://matplotlib.sf.net
And it comes with a windows installer for "lowly windows
developers".... Requires numeric or numarray.

JDH
 
M

Max M

John said:
In matplotlib, you can draw antialiased lines with any color, style,
and width you want. And we'll do the calculations for you (TM)

Ah great. Thank you. I will defintely need something along those lines
(ba-da-bum) in short time, so that is handy.

regards Max M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top