Browse Source

bugfix, antialias

master
Florin Tobler 3 years ago
parent
commit
b44b237e3f
  1. 16
      clock_pillow.py

16
clock_pillow.py

@ -19,10 +19,13 @@ def rrc(tuple, center, angle):
cos = math.cos(angle)
tpl = (tuple[0] - center[0], tuple[1] - center[1])
tpl = tuple
return (int(tpl[0] * sin + tpl[1] * cos + center[0]), int(tpl[1] * sin - tpl[0] * cos + center[1]))
return (int(tpl[0] * sin + tpl[1] * cos + center[0]), int(-tpl[0] * cos + tpl[1] * sin + center[1]))
def draw_clock(size=(800, 480), pixelformat="RGBA", r=210, mirror=False):
def draw_clock(size=(800, 480), pixelformat="RGBA", r=210, aliasing=None):
if aliasing != None:
size = (size[0] * aliasing, size[1] * aliasing)
r *= aliasing
image = Image.new(pixelformat, size)
#font = ImageFont.truetype(font="consola.ttf", size=40)
draw = ImageDraw.Draw(image)
@ -35,7 +38,7 @@ def draw_clock(size=(800, 480), pixelformat="RGBA", r=210, mirror=False):
for i5 in range(5):
angle = (i12 * 5 + i5) / 60 * 2 * math.pi
len = r * 0.93 if i5 == 0 else r * 0.97
thick = 5 if i5 == 0 else 2
thick = int((5 if i5 == 0 else 2) * r/180)
sin = math.sin(angle)
cos = math.cos(angle)
draw.line([(sin * len + center[0], cos * len + center[1]), (sin * r + center[0], cos * r + center[1])], fill=rgb(0.7), width=thick)
@ -53,7 +56,7 @@ def draw_clock(size=(800, 480), pixelformat="RGBA", r=210, mirror=False):
draw.line([rrc((+base, r * 0.2), center, angle), rrc((0, -r * 0.95), center, angle), rrc((-base, r * 0.2), center, angle)], fill=rgb(1), width=int(thick))
#draw second pointer
angle = (now.second + now.microsecond / 1000000) / 60 * 2 * math.pi / 2
angle = (now.second + now.microsecond / 1000000) / 60 * 2 * math.pi / 2 + math.pi / 2
thick = 4*r/180
draw.line([rrc((0, r * 0.3), center, angle), rrc((0, -r * 0.8), center, angle)], fill=rgb(1, 0, 0), width=int(thick))
e_center = rrc((0, -r * 0.8), center, angle)
@ -63,8 +66,11 @@ def draw_clock(size=(800, 480), pixelformat="RGBA", r=210, mirror=False):
e_r = r * 0.03
draw.ellipse([(e_center[0] - e_r, e_center[1] - e_r), (e_center[0] + e_r, e_center[1] + e_r)], fill=rgb(1, 0, 0), outline=None)
if aliasing != None:
image = image.resize((size[0] // aliasing, size[1] // aliasing))
return image
if __name__ == "__main__":
pil_image = draw_clock(pixelformat="RGB")
pil_image = draw_clock(pixelformat="RGB", aliasing=4)
pil_image.save("clock.png", format="png")
Loading…
Cancel
Save