mirror of
https://git.coom.tech/drummyfish/small3dlib.git
synced 2024-11-21 20:39:57 +01:00
Continue image tool
This commit is contained in:
parent
4354e98908
commit
d52845f836
1 changed files with 32 additions and 12 deletions
32
img2array.py
32
img2array.py
|
@ -9,12 +9,20 @@ from PIL import Image
|
||||||
def printHelp():
|
def printHelp():
|
||||||
print("Convert image to C array for small3dlib.")
|
print("Convert image to C array for small3dlib.")
|
||||||
print("usage:\n")
|
print("usage:\n")
|
||||||
print(" python img2array.py [TODO] file\n")
|
print(" python img2array.py [-xW -yH -h -nS -pT -5] file\n")
|
||||||
print(" TODO\n")
|
print(" -xW set width of the output to W pixels")
|
||||||
|
print(" -yH set height of the output to H pixels")
|
||||||
|
print(" -h include header guards (for texture per file)")
|
||||||
|
print(" -nS use the name S for the texture (defaut: \"texture\")")
|
||||||
|
print(" -pT use palette from file T and indexed colors (otherwise direct colors)")
|
||||||
|
print(" -5 use 565 format instead of RGB8")
|
||||||
print("");
|
print("");
|
||||||
print("by Miloslav \"drummyfish\" Ciz")
|
print("by Miloslav \"drummyfish\" Ciz")
|
||||||
print("released under CC0 1.0")
|
print("released under CC0 1.0")
|
||||||
|
|
||||||
|
def rgbTo565(rgb):
|
||||||
|
return ((rgb[0] >> 3) << 11) | ((rgb[1] >> 2) << 5) | ((rgb[2] >> 3))
|
||||||
|
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
printHelp()
|
printHelp()
|
||||||
quit()
|
quit()
|
||||||
|
@ -26,6 +34,7 @@ NAME = "texture"
|
||||||
GUARDS = False
|
GUARDS = False
|
||||||
OUT_WIDTH = 64
|
OUT_WIDTH = 64
|
||||||
OUT_HEIGHT = 64
|
OUT_HEIGHT = 64
|
||||||
|
USE_565 = False
|
||||||
|
|
||||||
for s in sys.argv:
|
for s in sys.argv:
|
||||||
if s [:2] == "-x":
|
if s [:2] == "-x":
|
||||||
|
@ -36,6 +45,8 @@ for s in sys.argv:
|
||||||
GUARDS = True
|
GUARDS = True
|
||||||
elif s[:2] == "-n":
|
elif s[:2] == "-n":
|
||||||
NAME = s[2:]
|
NAME = s[2:]
|
||||||
|
elif s[:2] == "-5":
|
||||||
|
USE_565 = True
|
||||||
elif s[:2] == "-p":
|
elif s[:2] == "-p":
|
||||||
PALETTE = s[2:]
|
PALETTE = s[2:]
|
||||||
USE_PALETTE = True
|
USE_PALETTE = True
|
||||||
|
@ -49,7 +60,7 @@ paletteArray = []
|
||||||
image = Image.open(FILENAME).convert("RGB")
|
image = Image.open(FILENAME).convert("RGB")
|
||||||
pixels = image.load()
|
pixels = image.load()
|
||||||
|
|
||||||
if len(PALETTE) > 0:
|
if USE_PALETTE > 0:
|
||||||
palette = Image.open(PALETTE).convert("RGB")
|
palette = Image.open(PALETTE).convert("RGB")
|
||||||
pixelsPal = palette.load()
|
pixelsPal = palette.load()
|
||||||
|
|
||||||
|
@ -57,6 +68,10 @@ if len(PALETTE) > 0:
|
||||||
for x in range(palette.size[0]):
|
for x in range(palette.size[0]):
|
||||||
c = pixelsPal[x,y]
|
c = pixelsPal[x,y]
|
||||||
paletteColors.append(c)
|
paletteColors.append(c)
|
||||||
|
|
||||||
|
if USE_565:
|
||||||
|
paletteArray.append(rgbTo565(c))
|
||||||
|
else:
|
||||||
paletteArray.append(c[0])
|
paletteArray.append(c[0])
|
||||||
paletteArray.append(c[1])
|
paletteArray.append(c[1])
|
||||||
paletteArray.append(c[2])
|
paletteArray.append(c[2])
|
||||||
|
@ -88,10 +103,14 @@ for y in range(OUT_HEIGHT):
|
||||||
|
|
||||||
imageArray.append(closestIndex)
|
imageArray.append(closestIndex)
|
||||||
pixels2[x,y] = paletteColors[closestIndex]
|
pixels2[x,y] = paletteColors[closestIndex]
|
||||||
|
else:
|
||||||
|
if USE_565:
|
||||||
|
imageArray.append(rgbTo565(pixel))
|
||||||
else:
|
else:
|
||||||
imageArray.append(pixel[0])
|
imageArray.append(pixel[0])
|
||||||
imageArray.append(pixel[1])
|
imageArray.append(pixel[1])
|
||||||
imageArray.append(pixel[2])
|
imageArray.append(pixel[2])
|
||||||
|
|
||||||
pixels2[x,y] = pixel
|
pixels2[x,y] = pixel
|
||||||
|
|
||||||
#-----------------------
|
#-----------------------
|
||||||
|
@ -109,12 +128,12 @@ def printArray(array, name, sizeString):
|
||||||
|
|
||||||
if lineLen > 80:
|
if lineLen > 80:
|
||||||
arrayString += "\n"
|
arrayString += "\n"
|
||||||
lineLen -= 80
|
lineLen = 0
|
||||||
|
|
||||||
arrayString += item
|
arrayString += item
|
||||||
|
|
||||||
print(arrayString[:-1])
|
print(arrayString[:-1])
|
||||||
print("}; // " + name + "\n")
|
print("}; // " + name)
|
||||||
|
|
||||||
if GUARDS:
|
if GUARDS:
|
||||||
print("#ifndef " + NAME.upper() + "_TEXTURE_H")
|
print("#ifndef " + NAME.upper() + "_TEXTURE_H")
|
||||||
|
@ -122,12 +141,13 @@ if GUARDS:
|
||||||
|
|
||||||
if USE_PALETTE:
|
if USE_PALETTE:
|
||||||
printArray(paletteArray,NAME + "Palette",str(len(paletteArray)))
|
printArray(paletteArray,NAME + "Palette",str(len(paletteArray)))
|
||||||
|
print("")
|
||||||
|
|
||||||
print("#define " + NAME.upper() + "_TEXTURE_WIDTH " + str(OUT_WIDTH))
|
print("#define " + NAME.upper() + "_TEXTURE_WIDTH " + str(OUT_WIDTH))
|
||||||
print("#define " + NAME.upper() + "_TEXTURE_HEIGHT " + str(OUT_HEIGHT))
|
print("#define " + NAME.upper() + "_TEXTURE_HEIGHT " + str(OUT_HEIGHT))
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
printArray(imageArray,NAME + "Texture",NAME.upper() + "_TEXTURE_WIDTH * " + NAME.upper() + "_TEXTURE_HEIGHT * 3")
|
printArray(imageArray,NAME + "Texture",str(len(imageArray)))
|
||||||
|
|
||||||
if GUARDS:
|
if GUARDS:
|
||||||
print("\n#endif // guard")
|
print("\n#endif // guard")
|
||||||
|
|
Loading…
Reference in a new issue