yclee126

Calendar Clock

Nov 30th, 2021 (edited)
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.73 KB | None | 0 0
  1. # Calendar Clock by yclee126
  2. # Just like a day, a year will pass.
  3. # https://youtu.be/MeSoWltxYbE
  4.  
  5. import numpy as np
  6. import cv2
  7. from time import sleep
  8. from calendar import monthrange
  9. import datetime
  10. from math import cos, sin, pi
  11.  
  12. import ctypes
  13. ctypes.windll.user32.SetProcessDPIAware()
  14.  
  15. img_w, img_h = 5000, 5000
  16. win_w, win_h = 700, 700
  17. wd2, hd2 = int(img_w/2), int(img_h/2)
  18.  
  19.  
  20. def putTextCenter(img, text, pos, font, size, color, thickness, line=cv2.LINE_4):
  21.     textsize = cv2.getTextSize(text, font, size, thickness)[0]
  22.     textX = round(pos[0] - textsize[0] / 2)
  23.     textY = round(pos[1] + textsize[1] / 2)
  24.     cv2.putText(img, text, (textX, textY), font, size, color, thickness, line)
  25.  
  26.  
  27. black = (0, 0, 0)
  28. white = (255, 255, 255)
  29. color_saturday = (255, 10, 10)
  30. color_sunday = (10, 10, 255)
  31.  
  32.  
  33. def main():
  34.     global today
  35.     img = np.zeros((img_h, img_w, 3), np.uint8)
  36.    
  37.     font = cv2.FONT_HERSHEY_TRIPLEX
  38.     clockface_dia = 200*10
  39.     days_dia = 170*10
  40.     months_dia = 100*10
  41.    
  42.     hand_offset = 20*10
  43.     hand_rev_length = 0
  44.     line_offset = 18*10
  45.     line_length = 5*10
  46.    
  47.     title = 'clock'
  48.     cv2.namedWindow(title)
  49.     while True:
  50.         # clear screen
  51.         cv2.rectangle(img, (0, 0), (img_w, img_h), white, -1)
  52.        
  53.         #get current time in UTC & draw it
  54.         #today = datetime.datetime.now(datetime.timezone.utc)
  55.         today = datetime.datetime.now()
  56.         putTextCenter(img, 'UTC+9', (wd2, 2900), font, 6, black, 10)
  57.        
  58.         # draw base clockface
  59.         cv2.circle(img, (wd2, hd2), clockface_dia, black, 20)
  60.         cv2.circle(img, (wd2, hd2), 7*10, black, -1)
  61.        
  62.         # draw days & hand
  63.         days = monthrange(today.year, today.month)[1]
  64.         for i in range(days):
  65.             rpos = i/days * 2*pi - pi/2
  66.             x, y = wd2 + cos(rpos)*days_dia, hd2 + sin(rpos)*days_dia
  67.             name = datetime.datetime(today.year, today.month, i+1).strftime("%A")
  68.             if name == 'Saturday':
  69.                 color = color_saturday
  70.             elif name == 'Sunday':
  71.                 color = color_sunday
  72.             else:
  73.                 color = black
  74.             putTextCenter(img, str(i+1), (x, y), font, 5.5, color, 12)  # TEXT
  75.             x0, y0 = int(wd2 + cos(rpos)*(days_dia+line_offset)), int(hd2 + sin(rpos)*(days_dia+line_offset))
  76.             x1, y1 = int(wd2 + cos(rpos)*(days_dia+line_offset+line_length)), int(hd2 + sin(rpos)*(days_dia+line_offset+line_length))
  77.             cv2.line(img, (x0, y0), (x1, y1), black, 10)
  78.         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
  79.         rpos = day_fpos * 2*pi - pi/2
  80.         vx, vy = cos(rpos), sin(rpos)
  81.         x0, y0 = int(wd2 + -vx*hand_rev_length), int(hd2 + -vy*hand_rev_length)
  82.         x1, y1 = int(wd2 + vx*(days_dia-hand_offset)), int(hd2 + vy*(days_dia-hand_offset))
  83.         cv2.line(img, (x0, y0), (x1, y1), black, 30)  # HAND
  84.        
  85.         # draw months & hand
  86.         for i in range(12):
  87.             rpos = i/12 * 2*pi - pi/2
  88.             x, y = wd2 + cos(rpos)*months_dia, hd2 + sin(rpos)*months_dia
  89.             putTextCenter(img, str(i+1), (x, y), font, 7, black, 12)  # TEXT
  90.         rpos = (today.month-1 + day_fpos)/12 * 2*pi - pi/2
  91.         vx, vy = cos(rpos), sin(rpos)
  92.         x0, y0 = int(wd2 + -vx*hand_rev_length), int(hd2 + -vy*hand_rev_length)
  93.         x1, y1 = int(wd2 + vx*(months_dia-hand_offset)), int(hd2 + vy*(months_dia-hand_offset))
  94.         cv2.line(img, (x0, y0), (x1, y1), black, 30)  # HAND
  95.        
  96.         # draw year
  97.         def draw_year(img, date):
  98.             text = str(date)
  99.             pos = (wd2, 2070)
  100.             size = 7
  101.             color = black
  102.             thickness = 10
  103.            
  104.             textsize = cv2.getTextSize(text, font, size, thickness)[0]
  105.             offsetX = round(textsize[0] / 2)
  106.             offsetY = round(textsize[1] / 2)
  107.             textX = pos[0] - offsetX
  108.             textY = pos[1] + offsetY
  109.             cv2.putText(img, text, (textX, textY), font, size, color, thickness)
  110.            
  111.             global_offset = 20
  112.             up, down, left, right = 20, 25, 7, 0  # edge offsets
  113.             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)
  114.         draw_year(img, today.year)
  115.        
  116.         # resize image
  117.         img2 = cv2.resize(img, dsize=(win_w, win_h), interpolation=cv2.INTER_AREA)
  118.         if cv2.getWindowProperty(title, 0) < 0:
  119.             break
  120.         cv2.imshow(title, img2)
  121.         key = cv2.waitKey(1000)
  122.  
  123.     cv2.destroyAllWindows()
  124.  
  125.  
  126. if __name__ == '__main__':
  127.     main()
Add Comment
Please, Sign In to add comment