Skip to content

Instantly share code, notes, and snippets.

@Andrew-William-Smith
Created August 23, 2017 19:00
Show Gist options
  • Save Andrew-William-Smith/90fcfbdc66865dcff77e250236b35711 to your computer and use it in GitHub Desktop.
Save Andrew-William-Smith/90fcfbdc66865dcff77e250236b35711 to your computer and use it in GitHub Desktop.
A script to map the luminance values of image pixels into three-dimensional space.
from PIL import Image
import sys
def rgb2hex(r, g, b):
return '#{:02x}{:02x}{:02x}'.format(r, g, b)
def printError(err):
print(err)
sys.exit(1)
try:
img = Image.open(sys.argv[1]).convert('RGB')
except IndexError:
printError('Error: No input file specified.\nUsage: python luminance-plot.py [input file] [output file] [striation interval]')
except IOError:
printError('Error: Input file "{}" not found.'.format(sys.argv[1]))
try: sys.argv[2]
except IndexError:
printError('Error: No output file specified.\nUsage: python luminance-plot.py [input file] [output file] [striation interval]')
try:
striation = int(sys.argv[3])
except ValueError:
printError('Error: Striation interval is not an integer.')
except IndexError:
printError('Error: No striation interval specified.\nUsage: python luminance-plot.py [input file] [output file] [striation interval]')
with open(sys.argv[2], 'w') as f:
for x in range(0, img.size[0]):
for y in range(0, img.size[1]):
r, g, b = img.getpixel((x, y))
luminosity = int(((0.241 * r**2)+(0.691 * g**2)+(0.068 * b**2)) ** 0.5)
for i in range(0, luminosity, striation):
f.write('{},{},{}\n'.format(str(x*2), str(i), str(y*2)))
f.write('{},{},{}\n'.format(str(x*2), str(luminosity), str(y*2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment