Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Calendar Clock by yclee126
- # Just like a day, a year will pass.
- # https://youtu.be/MeSoWltxYbE
- import numpy as np
- import cv2
- from time import sleep
- from calendar import monthrange
- import datetime
- from math import cos, sin, pi
- import ctypes
- ctypes.windll.user32.SetProcessDPIAware()
- img_w, img_h = 5000, 5000
- win_w, win_h = 700, 700
- wd2, hd2 = int(img_w/2), int(img_h/2)
- def putTextCenter(img, text, pos, font, size, color, thickness, line=cv2.LINE_4):
- textsize = cv2.getTextSize(text, font, size, thickness)[0]
- textX = round(pos[0] - textsize[0] / 2)
- textY = round(pos[1] + textsize[1] / 2)
- cv2.putText(img, text, (textX, textY), font, size, color, thickness, line)
- black = (0, 0, 0)
- white = (255, 255, 255)
- color_saturday = (255, 10, 10)
- color_sunday = (10, 10, 255)
- def main():
- global today
- img = np.zeros((img_h, img_w, 3), np.uint8)
- font = cv2.FONT_HERSHEY_TRIPLEX
- clockface_dia = 200*10
- days_dia = 170*10
- months_dia = 100*10
- hand_offset = 20*10
- hand_rev_length = 0
- line_offset = 18*10
- line_length = 5*10
- title = 'clock'
- cv2.namedWindow(title)
- while True:
- # clear screen
- cv2.rectangle(img, (0, 0), (img_w, img_h), white, -1)
- #get current time in UTC & draw it
- #today = datetime.datetime.now(datetime.timezone.utc)
- today = datetime.datetime.now()
- putTextCenter(img, 'UTC+9', (wd2, 2900), font, 6, black, 10)
- # draw base clockface
- cv2.circle(img, (wd2, hd2), clockface_dia, black, 20)
- cv2.circle(img, (wd2, hd2), 7*10, black, -1)
- # draw days & hand
- days = monthrange(today.year, today.month)[1]
- for i in range(days):
- rpos = i/days * 2*pi - pi/2
- x, y = wd2 + cos(rpos)*days_dia, hd2 + sin(rpos)*days_dia
- name = datetime.datetime(today.year, today.month, i+1).strftime("%A")
- if name == 'Saturday':
- color = color_saturday
- elif name == 'Sunday':
- color = color_sunday
- else:
- color = black
- putTextCenter(img, str(i+1), (x, y), font, 5.5, color, 12) # TEXT
- x0, y0 = int(wd2 + cos(rpos)*(days_dia+line_offset)), int(hd2 + sin(rpos)*(days_dia+line_offset))
- x1, y1 = int(wd2 + cos(rpos)*(days_dia+line_offset+line_length)), int(hd2 + sin(rpos)*(days_dia+line_offset+line_length))
- cv2.line(img, (x0, y0), (x1, y1), black, 10)
- day_fpos = (today.day-1 + (today.hour*60*60 + today.minute*60 + today.second)/(24*60*60))/days # also used to calculate month hand pos
- rpos = day_fpos * 2*pi - pi/2
- vx, vy = cos(rpos), sin(rpos)
- x0, y0 = int(wd2 + -vx*hand_rev_length), int(hd2 + -vy*hand_rev_length)
- x1, y1 = int(wd2 + vx*(days_dia-hand_offset)), int(hd2 + vy*(days_dia-hand_offset))
- cv2.line(img, (x0, y0), (x1, y1), black, 30) # HAND
- # draw months & hand
- for i in range(12):
- rpos = i/12 * 2*pi - pi/2
- x, y = wd2 + cos(rpos)*months_dia, hd2 + sin(rpos)*months_dia
- putTextCenter(img, str(i+1), (x, y), font, 7, black, 12) # TEXT
- rpos = (today.month-1 + day_fpos)/12 * 2*pi - pi/2
- vx, vy = cos(rpos), sin(rpos)
- x0, y0 = int(wd2 + -vx*hand_rev_length), int(hd2 + -vy*hand_rev_length)
- x1, y1 = int(wd2 + vx*(months_dia-hand_offset)), int(hd2 + vy*(months_dia-hand_offset))
- cv2.line(img, (x0, y0), (x1, y1), black, 30) # HAND
- # draw year
- def draw_year(img, date):
- text = str(date)
- pos = (wd2, 2070)
- size = 7
- color = black
- thickness = 10
- textsize = cv2.getTextSize(text, font, size, thickness)[0]
- offsetX = round(textsize[0] / 2)
- offsetY = round(textsize[1] / 2)
- textX = pos[0] - offsetX
- textY = pos[1] + offsetY
- cv2.putText(img, text, (textX, textY), font, size, color, thickness)
- global_offset = 20
- up, down, left, right = 20, 25, 7, 0 # edge offsets
- cv2.rectangle(img, (pos[0]-offsetX-(left+global_offset), pos[1]-offsetY-(up+global_offset)), (pos[0]+offsetX+(right+global_offset), pos[1]+offsetY+(down+global_offset)), black, 10)
- draw_year(img, today.year)
- # resize image
- img2 = cv2.resize(img, dsize=(win_w, win_h), interpolation=cv2.INTER_AREA)
- if cv2.getWindowProperty(title, 0) < 0:
- break
- cv2.imshow(title, img2)
- key = cv2.waitKey(1000)
- cv2.destroyAllWindows()
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment