2019-06-01 22:28:12 +02:00
|
|
|
# Python tool to convert an image to C array for small3dlib.
|
|
|
|
#
|
|
|
|
# by drummyfish
|
|
|
|
# released under CC0 1.0.
|
|
|
|
|
2019-06-02 20:22:42 +02:00
|
|
|
import sys
|
2019-06-01 22:28:12 +02:00
|
|
|
from PIL import Image
|
|
|
|
|
2019-06-02 20:22:42 +02:00
|
|
|
def printHelp():
|
|
|
|
print("Convert image to C array for small3dlib.")
|
|
|
|
print("usage:\n")
|
|
|
|
print(" python img2array.py [TODO] file\n")
|
|
|
|
print(" TODO\n")
|
|
|
|
print("");
|
|
|
|
print("by Miloslav \"drummyfish\" Ciz")
|
|
|
|
print("released under CC0 1.0")
|
|
|
|
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
printHelp()
|
|
|
|
quit()
|
|
|
|
|
|
|
|
FILENAME = ""
|
2019-06-03 13:08:08 +02:00
|
|
|
PALETTE = ""
|
|
|
|
USE_PALETTE = False
|
2019-06-02 20:22:42 +02:00
|
|
|
NAME = "texture"
|
|
|
|
GUARDS = False
|
|
|
|
OUT_WIDTH = 64
|
|
|
|
OUT_HEIGHT = 64
|
|
|
|
|
|
|
|
for s in sys.argv:
|
|
|
|
if s [:2] == "-x":
|
|
|
|
OUT_WIDTH = int(s[2:])
|
|
|
|
elif s [:2] == "-y":
|
|
|
|
OUT_HEIGHT = int(s[2:])
|
|
|
|
elif s == "-h":
|
|
|
|
GUARDS = True
|
|
|
|
elif s[:2] == "-n":
|
|
|
|
NAME = s[2:]
|
2019-06-03 13:08:08 +02:00
|
|
|
elif s[:2] == "-p":
|
|
|
|
PALETTE = s[2:]
|
|
|
|
USE_PALETTE = True
|
2019-06-02 20:22:42 +02:00
|
|
|
else:
|
|
|
|
FILENAME = s
|
2019-06-01 22:28:12 +02:00
|
|
|
|
2019-06-03 13:08:08 +02:00
|
|
|
imageArray = []
|
|
|
|
paletteColors = []
|
|
|
|
paletteArray = []
|
|
|
|
|
2019-06-01 22:28:12 +02:00
|
|
|
image = Image.open(FILENAME).convert("RGB")
|
|
|
|
pixels = image.load()
|
|
|
|
|
2019-06-03 13:08:08 +02:00
|
|
|
if len(PALETTE) > 0:
|
|
|
|
palette = Image.open(PALETTE).convert("RGB")
|
|
|
|
pixelsPal = palette.load()
|
|
|
|
|
|
|
|
for y in range(palette.size[1]):
|
|
|
|
for x in range(palette.size[0]):
|
|
|
|
c = pixelsPal[x,y]
|
|
|
|
paletteColors.append(c)
|
|
|
|
paletteArray.append(c[0])
|
|
|
|
paletteArray.append(c[1])
|
|
|
|
paletteArray.append(c[2])
|
|
|
|
|
2019-06-01 22:28:12 +02:00
|
|
|
image2 = Image.new("RGB",(OUT_WIDTH,OUT_HEIGHT),color="white")
|
|
|
|
pixels2 = image2.load()
|
|
|
|
|
2019-06-02 20:22:42 +02:00
|
|
|
for y in range(OUT_HEIGHT):
|
|
|
|
for x in range(OUT_WIDTH):
|
2019-06-01 22:28:12 +02:00
|
|
|
coord = (
|
|
|
|
int(x / float(OUT_WIDTH) * image.size[0]),
|
|
|
|
int(y / float(OUT_HEIGHT) * image.size[1]))
|
|
|
|
|
|
|
|
pixel = pixels[coord]
|
|
|
|
|
2019-06-03 13:08:08 +02:00
|
|
|
if USE_PALETTE:
|
|
|
|
closestIndex = 0
|
|
|
|
closestDiff = 1024
|
2019-06-01 22:28:12 +02:00
|
|
|
|
2019-06-03 13:08:08 +02:00
|
|
|
# find the index of the closest color:
|
2019-06-01 22:28:12 +02:00
|
|
|
|
2019-06-03 13:08:08 +02:00
|
|
|
for i in range(len(paletteColors)):
|
|
|
|
c = paletteColors[i]
|
|
|
|
diff = abs(pixel[0] - c[0]) + abs(pixel[1] - c[1]) + abs(pixel[2] - c[2])
|
2019-06-02 20:22:42 +02:00
|
|
|
|
2019-06-03 13:08:08 +02:00
|
|
|
if diff < closestDiff:
|
|
|
|
closestIndex = i
|
|
|
|
closestDiff = diff
|
2019-06-02 20:22:42 +02:00
|
|
|
|
2019-06-03 13:08:08 +02:00
|
|
|
imageArray.append(closestIndex)
|
|
|
|
pixels2[x,y] = paletteColors[closestIndex]
|
|
|
|
else:
|
|
|
|
imageArray.append(pixel[0])
|
|
|
|
imageArray.append(pixel[1])
|
|
|
|
imageArray.append(pixel[2])
|
|
|
|
pixels2[x,y] = pixel
|
2019-06-01 22:28:12 +02:00
|
|
|
|
2019-06-03 13:08:08 +02:00
|
|
|
#-----------------------
|
|
|
|
|
|
|
|
def printArray(array, name, sizeString):
|
|
|
|
print("uint8_t " + name + "[" + sizeString + "] = {")
|
|
|
|
arrayString = ""
|
|
|
|
|
|
|
|
lineLen = 0
|
2019-06-01 22:28:12 +02:00
|
|
|
|
2019-06-03 13:08:08 +02:00
|
|
|
for v in array:
|
|
|
|
item = str(v) + ","
|
2019-06-01 22:28:12 +02:00
|
|
|
|
|
|
|
lineLen += len(item)
|
|
|
|
|
|
|
|
if lineLen > 80:
|
|
|
|
arrayString += "\n"
|
|
|
|
lineLen -= 80
|
|
|
|
|
|
|
|
arrayString += item
|
|
|
|
|
2019-06-03 13:08:08 +02:00
|
|
|
print(arrayString[:-1])
|
|
|
|
print("}; // " + name + "\n")
|
|
|
|
|
|
|
|
if GUARDS:
|
|
|
|
print("#ifndef " + NAME.upper() + "_TEXTURE_H")
|
|
|
|
print("#define " + NAME.upper() + "_TEXTURE_H\n")
|
|
|
|
|
|
|
|
if USE_PALETTE:
|
|
|
|
printArray(paletteArray,NAME + "Palette",str(len(paletteArray)))
|
|
|
|
|
2019-06-03 17:07:40 +02:00
|
|
|
print("#define " + NAME.upper() + "_TEXTURE_WIDTH " + str(OUT_WIDTH))
|
|
|
|
print("#define " + NAME.upper() + "_TEXTURE_HEIGHT " + str(OUT_HEIGHT))
|
|
|
|
print("")
|
2019-06-03 13:08:08 +02:00
|
|
|
|
2019-06-03 17:07:40 +02:00
|
|
|
printArray(imageArray,NAME + "Texture",NAME.upper() + "_TEXTURE_WIDTH * " + NAME.upper() + "_TEXTURE_HEIGHT * 3")
|
2019-06-01 22:28:12 +02:00
|
|
|
|
2019-06-02 20:22:42 +02:00
|
|
|
if GUARDS:
|
|
|
|
print("\n#endif // guard")
|
|
|
|
|
|
|
|
image2.save(NAME + "_preview.png")
|