import math from PIL import Image,ImageDraw,ImageFont from pathlib import Path P=Path(__file__).parent cream='#f0d69b'; blue='#36525c'; red='#9e433b'; gold='#bb9246' S=600; C=S//2 font=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf',44) def dial(h): im=Image.new('RGB',(S,S),'#f8f2e5');d=ImageDraw.Draw(im);d.ellipse((7,7,593,593),fill=cream,outline=blue,width=9);d.ellipse((22,22,578,578),outline=red,width=3) for j in range(60): a=math.pi*j/30;v=(math.sin(a),-math.cos(a)); xy=lambda r:(C+r*v[0],C+r*v[1]);d.line([xy(262 if j%5==0 else 271),xy(276)],fill=blue,width=5 if j%5==0 else 2) if j%5==0:d.text(xy(231),str(j//5 or 12),fill=blue,font=font,anchor='mm') d.polygon([(294,300),(295,127),(300,102),(305,127),(306,300)],fill=blue) d.polygon([(300,293),(300+h*105,288),(300+h*147,300),(300+h*105,312),(300,307)],fill=red) d.ellipse((277,277,323,323),fill=gold,outline=blue,width=4);d.ellipse((294,294,306,306),fill='#ead5a0');return im im=dial(1);im.save(P/'three.png');other=dial(-1);other.save(P/'nine.png'); pix=im.load();pix2=other.load();ave={};av2={} def tally(acc,k,col): v=acc.setdefault(k,[0,0,0,0]);v[0]+=col[0];v[1]+=col[1];v[2]+=col[2];v[3]+=1 for y in range(S): for x in range(S): k=round(math.hypot(x-C,y-C));tally(ave,k,pix[x,y]);tally(av2,k,pix2[x,y]) print('Largest difference in mean channel at any rounded radius:',max(abs(a-b)/ave[k][3] for k in ave for a,b in zip(ave[k][:3],av2[k][:3]))) assert ave == av2, 'The tops no longer paint the same rings.' mean={k:tuple(round(c/a[3]) for c in a[:3]) for k,a in ave.items()};out=im.copy();op=out.load() for y in range(S): for x in range(S): op[x,y]=mean[round(math.hypot(x-C,y-C))] out.save(P/'turn.png')