Advertisement
skaramicke

glow effect

Nov 14th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. width = 200
  2. height = 200
  3. passes = 10
  4. sample_diameter = 6
  5.  
  6. import operator
  7. from datetime import datetime
  8. from PIL import Image, ImageDraw
  9. from math import sin, cos, hypot
  10.  
  11. image = Image.new('RGB', (width, height), (0, 0, 0, 255))
  12. draw = ImageDraw.Draw(image, 'RGBA')
  13.  
  14. for x in range(width * 300):
  15.     y = int(round(sin(float(x)/20.0)*height/4.0+height/2.0))
  16.     draw.point((x, y), fill=(255, int(round(255*cos(float(y)/50))), int(round(255*sin(float(x)/100))), 255))
  17.  
  18. draw.line((0, height / 4, width, height / 2+1))
  19. draw.line((0, height / 4*3, width, height / 2+2))
  20. draw.line((0, height / 5*4, width, height / 2+3))
  21.  
  22. def blur():
  23.     tmp_image = Image.new('RGBA', (width, height), (0, 0, 0, 0))
  24.     tmp_draw = ImageDraw.Draw(tmp_image, 'RGBA')
  25.     for x in range(width):
  26.         for y in range(height):
  27.             pixel = image.getpixel((x, y))
  28.  
  29.             if pixel != (0, 0, 0):
  30.                 continue
  31.  
  32.             number_of_samples = 0
  33.             sample_sum = (0, 0, 0)
  34.             sample_radius = float(sample_diameter)/2.0
  35.             xmin = max(0, int(round(x - sample_radius)))
  36.             xmax = min(width, int(round(x + sample_radius)))
  37.             ymin = max(0, int(round(y - sample_radius)))
  38.             ymax = min(height, int(round(y + sample_radius)))
  39.             smallest_value = (255, 255, 255)
  40.             for sample_x in range(xmin, xmax):
  41.                 for sample_y in range(ymin, ymax):
  42.                     if hypot(sample_x-x, sample_y-y) > sample_radius:
  43.                         continue
  44.                     number_of_samples += 1
  45.                     sample_pixel = image.getpixel((sample_x, sample_y))
  46.                     if sum(sample_pixel) > 0 and sum(smallest_value) > sum(sample_pixel):
  47.                         smallest_value = sample_pixel
  48.                     sample_sum = tuple(map(operator.add, sample_sum, sample_pixel))
  49.            
  50.             if sample_sum != (0, 0, 0):
  51.                 c = (int(round(sample_sum[0] / float(number_of_samples) + float(smallest_value[0])/5)),
  52.                      int(round(sample_sum[1] / float(number_of_samples) + float(smallest_value[1])/5)),
  53.                      int(round(sample_sum[2] / float(number_of_samples) + float(smallest_value[2])/5)))
  54.  
  55.                 tmp_draw.point( (x, y), fill=(c[0], c[1], c[2], 255) )
  56.  
  57.     image.paste(tmp_image, (0,0), tmp_image)
  58.  
  59. print 'Started at ' + datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  60.  
  61. for i in range(passes):
  62.     blur()
  63.  
  64. image.show()
  65. image.save(datetime.now().strftime("glow_%Y-%m-%d_%H.%M.%S") + '.bmp')
  66.  
  67. print 'Finished at ' + datetime.now().strftime("%Y-%m-%d %H:%M:%S")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement