-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixer_image.py
More file actions
29 lines (27 loc) · 848 Bytes
/
pixer_image.py
File metadata and controls
29 lines (27 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import json
import sys
from PIL import Image
def main():
if len(sys.argv) < 4:
print "Too few arguments passed"
return
filename = sys.argv[1]
width = int(sys.argv[2])
height = int(sys.argv[3])
if len(sys.argv) > 4:
set_value = True
im = Image.open(filename).convert('L').resize((width, height))
pixels = list(im.getdata())
pixer_coords = []
threshold = 150
for y in xrange(height):
for x in xrange(width):
idx = width * y + x
val = pixels[idx] if set_value else 1
if val < threshold:
pixer_coords.append({'x': x, 'y': y, 'value': 255 - val})
name, _ = filename.split('.', 1)
with open(name + '.js', 'w') as f:
f.write('var ' + name + ' = ' + json.dumps(pixer_coords))
if __name__ == '__main__':
main()