Skip to content

Instantly share code, notes, and snippets.

@johann2
Created November 13, 2015 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johann2/5ff1546548cc7b2d3144 to your computer and use it in GitHub Desktop.
Save johann2/5ff1546548cc7b2d3144 to your computer and use it in GitHub Desktop.
Generate light textures
from PIL import Image
import math
width=255;
height=255;
center_x=width/2.0
center_y=height/2.0
max_distance=min(center_x,center_y)
img = Image.new( 'RGB', (width,height), "black")
pixels = img.load()
#linear falloff
for i in range(img.size[0]):
for j in range(img.size[1]):
distance_from_center=math.sqrt(math.pow(i-center_x,2)+math.pow(j-center_y,2))
normalized_distance=distance_from_center/max_distance;
brightness=0
if normalized_distance<1.0:
brightness = 1.0-normalized_distance
brightness = int(brightness*255);
pixels[i,j] = (brightness, brightness,brightness)
img.show()
img.save("linear.png")
#quadratic falloff
img2 = Image.new( 'RGB', (width,height), "black")
pixels2 = img2.load()
for i in range(img2.size[0]):
for j in range(img2.size[1]):
distance_from_center=math.sqrt(math.pow(i-center_x,2)+math.pow(j-center_y,2))
normalized_distance=distance_from_center/max_distance;
brightness=0
if normalized_distance<1.0:
brightness = 1.0-normalized_distance
brightness = math.pow(brightness,2) #!!!!
brightness = int(brightness*255);
pixels2[i,j] = (brightness,brightness,brightness)
img2.save("quadratic.png")
img2.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment