what I’m trying to do is to draw horizontal lines in PIL of equal size. but my problem is that when I’m trying to fill the image it isn’t full. This is what I’m getting when it should be fully equal. There shouldn’t be any white. It also should work with more or less colors
Code
JavaScript
x
22
22
1
from PIL import Image, ImageDraw
2
import random
3
4
im = Image.new('RGB', (1000, 1000), (255, 255, 255))
5
draw = ImageDraw.Draw(im)
6
colors = [(255,0,0), (255,255,0)]
7
8
n = 0
9
length = len(colors)
10
amount = 1000 / length
11
x1 = 0
12
y1 = 0
13
x2 = 1000
14
y2 = 0
15
for color in colors:
16
shape = [(x1, y1), x2, y2]
17
draw.line(shape, fill=color, width=int(amount))
18
y1 += amount
19
y2 += amount
20
21
im.save('rect.png')
22
Advertisement
Answer
When drawing the line, you are specifying the center of the line, so your “shape” should be:
JavaScript
1
2
1
shape = [(x1, y1 + amount // 2), (x2, y2 + amount // 2)]
2