hemispheres.html


001| ####################################################################################################
002| from sys import argv, stdout
003| from os import path
004| from math import sqrt, cos,pi
005| from random import random, choice
006| from PIL import Image, ImageDraw
007| from colors import ColorPalette
008| ####################################################################################################
009| BasePath = r'C:\CJS\prj\Python\ws\PIL'
010| ImgsPath = path.join(BasePath, 'img')
011| Filename = 'hemispheres.png'
012| 
013| dims = (1200, 800)
014| center = dims[0]//2, dims[1]//2
015| 
016| ##================================================================================================##
017| def draw_hemisphere (cp, rad, ball, falloff=2):
018|     """Draw a shaded hemisphere with rad=radius at cp=(x,y)."""
019|     cf = lambda a,ix:int(ball[ix] - ((1-a)*(ball[ix]//2)))
020|     af = lambda d,r:(255 if d<(r-1.5) else 191)
021| 
022|     dia = 1 + int(2 * rad)
023| 
024|     im2 = Image.new('RGBA', (dia,dia), color=(0,0,0,0))
025|     gi2 = ImageDraw.Draw(im2)
026| 
027|     # Draw a hemisphere...
028|     for px in range(dia):
029|         for py in range(dia):
030|             dist = sqrt(pow(px-rad,2) + pow(py-rad,2))
031|             if dist <= rad:
032|                 f = pow(cos((dist/rad)*(pi/2)), falloff)
033|                 color = (cf(f,0), cf(f,1), cf(f,2), af(dist,rad))
034|                 gi2.point([px,py], fill=color)
035| 
036|     # Blend hemisphere image to main image...
037|     im.alpha_composite(im2, (int(cp[0]-rad), int(cp[1]-rad)))
038| 
039| ##================================================================================================##
040| def artwork (n=256, margin=30, minrad=10, maxrad=100):
041|     """Draw a bunch of hemispheres in random sizes and colors."""
042|     print()
043|     stdout.flush()
044| 
045|     # Draw N hemispheres...
046|     for ix in range(n):
047|         x = (margin + ((dims[0]-(2*margin)) * random()))
048|         y = (margin + ((dims[1]-(2*margin)) * random()))
049|         r = minrad + ((maxrad-minrad) * random())
050|         c = ColorPalette[ix % len(ColorPalette)] #choice(ColorPalette)
051|         draw_hemisphere((x,y), r, c, falloff=2)
052|         stdout.write('%d ' % ix)
053|         if (ix % 101) == 0: stdout.write('\n')
054| 
055|     stdout.write('\n')
056|     stdout.flush()
057|     print()
058| 
059| ##================================================================================================##
060| def test ():
061|     im = Image.new('RGBA', dims, color=(255,255,255,255))
062| 
063|     #im2 = Image.new('RGBA', (180,100), color=(0,0,0,127))
064|     #im.alpha_composite(im2, (200,200), (10,10))
065| 
066|     fn = path.join(BasePath, Filename)
067|     im.save(fn)
068|     print('wrote: %s' % fn)
069|     print()
070| 
071| 
072| ##================================================================================================##
073| def main (*args):
074|     #draw_hemisphere ((100,100), 100, (255,63,63))
075|     #draw_hemisphere ((200,150), 100, (31,63,255))
076| 
077|     #artwork(n=5000, margin=0, minrad=5, maxrad=50)
078| 
079|     test()
080|     return ()
081| ####################################################################################################
082| if __name__ == '__main__':
083|     print(argv[0])
084|     print()
085|     rv = main(*argv[1:])
086|     print()
087| ####################################################################################################
088| '''eof'''
089|