Florin Andrei
Florin Andrei

Reputation: 1097

Python ImageDraw - how to draw a thick line (width or thickness more than 1 px)

Simple problem: Using the ImageDraw module in Python, draw a line between (x1, y1) and (x2, y2) with a thickness or width larger than 1 pixel.

Upvotes: 0

Views: 2677

Answers (1)

Florin Andrei
Florin Andrei

Reputation: 1097

Quote from actual script, showing only the part actually involved in drawing the thick line:

from PIL import Image, ImageDraw
import math

x1 = 100
y1 = 100
x2 = 200
y2 = 175

# thickness of line
thick = 4

# compute angle
a = math.atan((y2-y1)/(x2-x1))
sin = math.sin(a)
cos = math.cos(a)
xdelta = sin * thick / 2.0
ydelta = cos * thick / 2.0
xx1 = x1 - xdelta
yy1 = y1 + ydelta
xx2 = x1 + xdelta
yy2 = y1 - ydelta
xx3 = x2 + xdelta
yy3 = y2 - ydelta
xx4 = x2 - xdelta
yy4 = y2 + ydelta
draw.polygon((xx1, yy1, xx2, yy2, xx3, yy3, xx4, yy4))

Here's a result of this technique. The segments composing the dial are each drawn using the "thick line" technique.

dial.png

EDIT: This is the discussion that initiated my search for a "thick line" function in Python (also contains the full script I wrote):

http://gimpforums.com/thread-how-to-draw-this-geometric-pattern-programmatically

Upvotes: 2

Related Questions