-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrgb.py
32 lines (25 loc) · 819 Bytes
/
rgb.py
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
30
31
32
#image to rgb triplets
#generate RGB pixel color data from an image, for use with Tidbyt sprites
#by Kay Savetz
import sys
from PIL import Image
if len(sys.argv) <= 1:
print("Usage: " + sys.argv[0] + " imagefile")
exit(1)
img = Image.open(sys.argv[1])
print(img.format, img.size, img.mode)
for y in range(img.size[1]):
print ("sprite.append([",end='')
for x in range((img.size[0])):
pixel = img.getpixel((x,y))
if len(pixel) == 3:
(r, g, b) = pixel
else:
(r, g, b, a) = pixel
r = hex(int(r / 16))
g = hex(int(g / 16))
b = hex(int(b / 16))
print ('"#' + str(r).upper()[2:] + (str(g).upper()[2:]) + (str(b)[2:].upper()) + '"', end='')
if(x != img.size[0] -1):
print(",",end='')
print('])')