Line Example#
Naive Lines and Gap#
As a starting point create two vertical lines 15 pixels long and 15 pixels apart. Start from the coordinate (30, 10). You should have a script looking something like the start of the following.
Show/Hide Code 00test_line_start_finish.py
import sys
sys.path.append('../dims')
from PIL import Image, ImageDraw, ImageFont
from DimLinesPIL import angled_text, dims
Font = ImageFont.truetype('consola.ttf', 12)
w, h = 100, 100
image = Image.new('RGB', (w,h), '#FFFFDD')
a = (30, 10)
b = (30, 25)
c = (30, 40)
d = (30, 55)
draw = ImageDraw.Draw(image)
draw.line([a, b], fill='black')
draw.line([c, d], fill='black')
# width, height = Font.getsize(str(d))
unused1, unused2, width, height = Font.getbbox(str(d))
angled_text(image, (a[0] + 10 + width//2, a[1]),text=str(a), angle=0, fill='black',
font=Font)
angled_text(image, (b[0] + 10 + width//2, b[1]),text=str(b), angle=0, fill='black',
font=Font)
angled_text(image, (c[0] + 10 + width//2, c[1]),text=str(c), angle=0, fill='black',
font=Font)
angled_text(image, (d[0] + 10 + width//2, d[1]),text=str(d), angle=0, fill='black',
font=Font)
dims(image, draw, a, b, (8, 2), extB=(8, 2), text='16', font=Font, fill='red',
textorient='horizontal')
dims(image, draw, b, c, (8, 2), extB=(8, 2), text='14', font=Font, fill='red',
textorient='horizontal')
dims(image, draw, c, d, (8, 2), extB=(8, 2), text='16', font=Font, fill='red',
textorient='horizontal')
image.show()
Look carefully at the result, enlarge it so that you can count the number of pixels. Probably the line sizes are too long and the space is too short.
Drawing two lines and gap with equally spaced coordinates#
Although the coordinates are 15 pixels apart, on inspection the lines are 16 pixels long and the gap 14 pixels.
Enforce Correct Line and Gap Sizes#
Change the coordinates to achieve equal spacing on the resulting drawing. Add an extra gap and line, to give six coordinates. Alter the first script to give something like.
Show/Hide Code 01line_start_finish.py
import sys
sys.path.append('../dims')
from PIL import Image, ImageDraw, ImageFont
from DimLinesPIL import angled_text, dims
Font = ImageFont.truetype('consola.ttf', 12)
w, h = 100, 100
image = Image.new('RGB', (w,h), '#FFFFDD')
a = (30, 10)
b = (30, 24)
c = (30, 40)
d = (30, 54)
e = (30, 70)
f = (30, 84)
draw = ImageDraw.Draw(image)
draw.line([a, b], fill='black')
draw.line([c, d], fill='black')
draw.line([e,f], fill='black')
# width, height = Font.getsize(str(d))
unused1, unused2, width, height = Font.getbbox(str(d))
angled_text(image, (a[0] + 10 + width//2, a[1]),text=str(a), angle=0, fill='black',
font=Font)
angled_text(image, (b[0] + 10 + width//2, b[1]),text=str(b), angle=0, fill='black',
font=Font)
angled_text(image, (c[0] + 10 + width//2, c[1]),text=str(c), angle=0, fill='black',
font=Font)
angled_text(image, (d[0] + 10 + width//2, d[1]),text=str(d), angle=0, fill='black',
font=Font)
angled_text(image, (e[0] + 10 + width//2, e[1]),text=str(e), angle=0, fill='black',
font=Font)
angled_text(image, (f[0] + 10 + width//2, f[1]),text=str(f), angle=0, fill='black',
font=Font)
dims(image, draw, a, b, (8, 2), extB=(8, 2), text='16', font=Font, fill='lightblue',
textorient='horizontal')
dims(image, draw, b, c, (8, 2), extB=(8, 2), text='14', font=Font, fill='lightblue',
textorient='horizontal')
dims(image, draw, c, d, (8, 2), extB=(8, 2), text='16', font=Font, fill='lightblue',
textorient='horizontal')
dims(image, draw, d, e, (8, 2), extB=(8, 2), text='14', font=Font, fill='lightblue',
textorient='horizontal')
dims(image, draw, e, f, (8, 2), extB=(8, 2), text='16', font=Font, fill='lightblue',
textorient='horizontal')
image.show()
Look at the pairs of coordinates, the first pair is the same as before, but the second pair is changed by one pixel. This shortens the line and increases the gap. As it happens both changes cancel each other out when we come to the start of the next pair of coordinates, which in their turn need to be adjusted.
Drawing three dashes and two gaps of equal size#
The last coordinates in each pair are adjusted, resulting in lines and gaps each 15 pixels in size.
Show/Hide Code 02line_start_finish.py
import sys
sys.path.append('../dims')
from PIL import Image, ImageDraw, ImageFont
from DimLinesPIL import angled_text, dims
Font = ImageFont.truetype('consola.ttf', 12)
w, h = 100, 100
image = Image.new('RGB', (w,h), '#FFFFDD')
a = (30, 10)
b = (30, 24)
c = (30, 40)
d = (30, 54)
e = (30, 70)
f = (30, 84)
draw = ImageDraw.Draw(image)
draw.line([a, b], fill='black')
draw.line([c, d], fill='black')
draw.line([e,f], fill='black')
# width, height = Font.getsize(str(d))
unused1, unused2, width, height = Font.getbbox(str(d))
angled_text(image, (a[0] + 10 + width//2, a[1]),text=str(a), angle=0, fill='black',
font=Font)
angled_text(image, (b[0] + 10 + width//2, b[1]),text=str(b), angle=0, fill='black',
font=Font)
angled_text(image, (c[0] + 10 + width//2, c[1]),text=str(c), angle=0, fill='black',
font=Font)
angled_text(image, (d[0] + 10 + width//2, d[1]),text=str(d), angle=0, fill='black',
font=Font)
angled_text(image, (e[0] + 10 + width//2, e[1]),text=str(e), angle=0, fill='black',
font=Font)
angled_text(image, (f[0] + 10 + width//2, f[1]),text=str(f), angle=0, fill='black',
font=Font)
dims(image, draw, a, b, (8, 2), extB=(8, 2), text='15', font=Font, fill='lightblue',
textorient='horizontal')
dims(image, draw, b, c, (8, 2), extB=(8, 2), text='15', font=Font, fill='lightblue',
textorient='horizontal')
dims(image, draw, c, d, (8, 2), extB=(8, 2), text='15', font=Font, fill='lightblue',
textorient='horizontal')
dims(image, draw, d, e, (8, 2), extB=(8, 2), text='15', font=Font, fill='lightblue',
textorient='horizontal')
dims(image, draw, e, f, (8, 2), extB=(8, 2), text='15', font=Font, fill='lightblue',
textorient='horizontal')
image.show()