watchface.html


001| ####################################################################################################
002| from sys import argv
003| from os import path
004| from math import pi, sin,cos
005| from cmath import exp
006| from datetime import datetime
007| from PIL import Image, ImageDraw, ImageFont
008| ####################################################################################################
009| BasePath = r'C:\CJS\prj\Python\app'
010| ImgsPath = path.join(BasePath, 'img')
011| 
012| dims = (6000, 6000)
013| lims = ((-1.15,+1.15), (-1.15,+1.15))
014| 
015| draw_seconds   = True
016| draw_diagonals = False
017| 
018| shrink_image = True
019| shrink_size = (600, 600)
020| 
021| ##================================================================================================##
022| def main (*args):
023|     outname = path.join(ImgsPath, 'watchface.png' if shrink_image else 'watchface-big.png')
024| 
025|     frame = (lims[0][1]-lims[0][0], lims[1][1]-lims[1][0])
026|     scale = (dims[0]//frame[0], dims[1]//frame[1])
027|     edge  = (dims[0]-1, dims[1]-1)
028| 
029|     outer_radius = 1.25
030|     ticks_radius = 0.96
031|     digit_radius = 0.86
032|     hours_radius = 0.60
033|     mins_radius  = 0.80
034| 
035|     eye = complex(0, 1)
036|     ipi = complex(pi,0) * eye
037| 
038|     px = lambda x: ((x * scale[0]) + dims[0]//2)    # convert virtual X to pixels
039|     py = lambda y: (dims[1]//2 - (y * scale[1]))    # convert virtual Y to pixels
040| 
041|     z2t = lambda z:(px(z.real), py(z.imag))     # convert z to a tuple
042|     ulc = lambda p,d:(p[0]-d, p[1]-d)           # offset ULC from center
043|     lrc = lambda p,d:(p[0]+d, p[1]+d)           # offset LRC from center
044| 
045|     euler = lambda a:complex(cos(a), sin(a))    # z=(cos(angle),i*sin(angle))
046|     clock = lambda n:(12 - ((n-3) % 12))
047| 
048| 
049|     # MAKE AN IMAGE OF THE CIRCLE...
050|     im = Image.new('RGB', dims, color=(255,255,255))
051|     gi = ImageDraw.Draw(im)
052| 
053|     fnt0 = ImageFont.truetype('corbelb.ttf', size=440)
054|     fnt1 = ImageFont.truetype('corbeli.ttf', size=200)
055| 
056|     # Circles...
057|     def draw_circle (radius=1.0, parts=500, width=7, color=(127,127,255), stop=None):
058|         ang = (2.0 * pi)/parts
059|         rot = euler(ang)
060|         dot = complex(radius, 0)
061|         # Generate the circle points...
062|         point_list = [dot]
063|         for ix in range(parts):
064|             pt = point_list[-1] * rot
065|             point_list.append(pt)
066|             if stop and ((parts*stop) <= ix):
067|                 break
068|         if stop is None:
069|             point_list.append(dot)
070|         # Draw the circle...
071|         gi.line([z2t(z) for z in point_list], width=width, fill=color)
072| 
073|     draw_circle(radius=1.0, width=69, color=(223,223,255))
074|     draw_circle(radius=1.0, width=61, color=(207,207,255))
075|     draw_circle(radius=1.0, width=53, color=(191,191,255))
076|     draw_circle(radius=1.0, width=45, color=(159,159,255))
077|     draw_circle(radius=1.0, width=37, color=(127,127,255))
078| 
079|     # "Company Name"...
080|     txt = 'Smytheco'
081|     bb = gi.textbbox((0,0), txt, font=fnt1)
082|     x,y = (px(0), py(-0.07))
083|     gi.text((x-((bb[2]-bb[0])//2),y), txt, font=fnt1, fill=(127,127,127))
084| 
085|     # Clock ticks...
086|     for ang in [n/30.0 for n in range(60)]:
087|         pt = z2t(ticks_radius*exp(ipi*ang))
088|         gi.ellipse([ulc(pt,20),lrc(pt,20)], fill=(255,0,0), outline=(0,0,0), width=3)
089| 
090|     # Clock numbers...
091|     for ix,ang in enumerate([n/6.0 for n in range(12)]):
092|         x,y = z2t(digit_radius*exp(ipi*ang))
093|         txt = '%d' % clock(ix)
094|         bb = gi.textbbox((0,0), txt, font=fnt0)
095|         gi.text(((x-((bb[2]-bb[0])//2)), (y-(bb[3]-bb[1]))), txt, font=fnt0, fill=(0,0,0))
096| 
097|     # Clock hands...
098|     def hand_hours (t):
099|         ang = clock(t)/6.0
100|         pt0 = (px(0), py(0))
101|         pt1 = z2t(hours_radius*exp(ipi*ang))
102|         gi.line([pt0, pt1], width=121, fill=(0,0,0))
103| 
104|     def hand_minutes (t):
105|         ang = ((60 - ((t-15) % 60)) / 30)
106|         pt0 = (px(0), py(0))
107|         pt1 = z2t(mins_radius*exp(ipi*ang))
108|         gi.line([pt0, pt1], width=57, fill=(0,0,0))
109| 
110|     def hand_seconds (t):
111|         ang = ((60 - ((t-15) % 60)) / 30)
112|         pt0 = (px(0), py(0))
113|         pt1 = z2t(mins_radius*exp(ipi*ang))
114|         gi.line([pt0, pt1], width=33, fill=(0x33,0x66,0xcc))
115| 
116|     hand_hours(t_hours)
117|     hand_minutes(t_mins)
118|     if draw_seconds: hand_seconds(t_secs)
119| 
120| 
121|     # Diagonals...
122|     def draw_diagonal (angle, width=5, color=(63,159,63)):
123|         pt0 = outer_radius * exp(ipi*angle)
124|         pt1 = complex(0,0)
125|         gi.line([z2t(pt0), z2t(pt1)], width=width, fill=color)
126| 
127|     if draw_diagonals:
128|         draw_diagonal(clock(12)/6, width=25, color=(0,0x7f,0x3f))
129|         draw_diagonal(clock(t_hours)/6, width=25, color=(0,0x7f,0x3f))
130|         draw_diagonal(clock(p_south)/6, width=35, color=(0xff,0,0))
131| 
132| 
133|     # Center dot...
134|     pt = (px(0), py(0))
135|     gi.ellipse([ulc(pt,100),lrc(pt,100)], fill=(0,0,0), outline=(0,0,0), width=3)
136| 
137|     # Border...
138|     border_width = 21
139|     border_lines = [(0,0), (edge[0],0), (edge[0],edge[1]), (0,edge[1]), (0,0)]
140|     shift_templt = [(+1,+1), (-1,+1), (-1,-1), (+1,-1), (+1,+1)]
141|     border_color = (0, 0, 0)
142| 
143|     for ix in range(border_width):
144|         gi.line(border_lines, fill=border_color, width=1)
145|         border_lines = [(b[0]+s[0], b[1]+s[1]) for b,s in zip(border_lines,shift_templt)]
146|         border_color = tuple([c+8 for c in border_color])
147| 
148| 
149|     # Save the image file...
150|     if shrink_image:
151|         im = im.resize(shrink_size)
152|     im.save(outname)
153|     print()
154|     print('wrote: %s' % outname)
155|     print('wrote: %d:%02d:%02d [%d]' % (t_hours,t_mins,t_secs,p_south))
156|     print()
157|     print('dims=%s' % str(dims))
158|     print('lims=%s' % str(lims))
159|     print('frame=%s' % str(frame))
160|     print('scale=%s' % str(scale))
161|     print()
162| 
163| ####################################################################################################
164| if __name__ == "__main__":
165|     print('autorun: %s' % argv[0])
166| 
167|     Now = datetime.now()
168| 
169|     t_hours = int(argv[1]) if 1 < len(argv) else (Now.hour + (Now.minute/60) + (Now.second/3600))
170|     t_mins  = int(argv[2]) if 2 < len(argv) else (Now.minute + (Now.second/60))
171|     t_secs  = int(argv[3]) if 3 < len(argv) else Now.second
172|     p_south = int(argv[4]) if 4 < len(argv) else 6+int(t_hours/2)
173| 
174|     rv = main(*argv[1:])
175|     print()
176| ####################################################################################################
177| '''eof'''
178|