commit
04e0a4fb28
1 changed files with 82 additions and 0 deletions
@ -0,0 +1,82 @@ |
|||
#from PIL import Image, ImageDraw |
|||
from datetime import datetime |
|||
import math |
|||
import cairo |
|||
|
|||
|
|||
def draw_clock(size=(800, 480), r=210, mirror=False): |
|||
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, size[0], size[1]) |
|||
context = cairo.Context(surface) |
|||
now = datetime.now() |
|||
|
|||
# im = Image.new('RGBA', size, (0, 0, 0, 255)) |
|||
context.save() |
|||
context.translate(size[0]/2, size[1]/2) #translate to center |
|||
if mirror: |
|||
context.scale(-1, 1) |
|||
|
|||
#draw watch face |
|||
context.set_source_rgb(0.7, 0.7, 0.7) |
|||
for i12 in range(12): |
|||
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 |
|||
context.save() |
|||
context.set_line_width(thick*r/180) |
|||
context.rotate(angle) |
|||
context.move_to(0, r) |
|||
context.line_to(0, len) |
|||
context.stroke() |
|||
context.restore() |
|||
|
|||
#draw hour pointer |
|||
base = r * 0.015 |
|||
context.save() |
|||
context.rotate((now.hour + (now.minute + now.second / 60) / 60) / 12 * 2 * math.pi) |
|||
context.set_line_width(10*r/180) |
|||
context.set_source_rgb(1, 1, 1) |
|||
context.move_to(+base, r * 0.2) |
|||
context.line_to(0, -r * 0.65) |
|||
context.line_to(-base, r * 0.2) |
|||
context.stroke() |
|||
context.restore() |
|||
|
|||
#draw minute pointer |
|||
base = r * 0.02 |
|||
context.save() |
|||
context.rotate((now.minute + now.second / 60) / 60 * 2 * math.pi) |
|||
context.set_line_width(8*r/180) |
|||
context.set_source_rgb(1, 1, 1) |
|||
context.move_to(+base, r * 0.2) |
|||
context.line_to(0, -r * 0.95) |
|||
context.line_to(-base, r * 0.2) |
|||
context.stroke() |
|||
context.restore() |
|||
|
|||
#draw second pointer |
|||
context.save() |
|||
context.rotate((now.second + now.microsecond / 1000000) / 60 * 2 * math.pi) |
|||
context.set_line_width(4*r/180) |
|||
context.set_source_rgb(1, 0, 0) |
|||
context.move_to(0, r * 0.3) |
|||
context.line_to(0, -r * 0.8) |
|||
context.stroke() |
|||
context.arc(0, -r * 0.8, r * 0.06, 0, 2*math.pi) |
|||
context.fill() |
|||
context.arc(0, 0, r * 0.03, 0, 2*math.pi) |
|||
context.fill() |
|||
context.restore() |
|||
|
|||
|
|||
context.restore() |
|||
|
|||
return surface |
|||
|
|||
|
|||
|
|||
if __name__ == "__main__": |
|||
draw_clock().write_to_png("clock.png") |
|||
# import matplotlib.pyplot as plt |
|||
# plt.imshow(draw_clock()) |
|||
# plt.show() |
Loading…
Reference in new issue