mirror of
https://git.coom.tech/drummyfish/small3dlib.git
synced 2024-12-22 01:26:17 +01:00
Continue image tool
This commit is contained in:
parent
4354e98908
commit
d52845f836
1 changed files with 32 additions and 12 deletions
44
img2array.py
44
img2array.py
|
@ -9,12 +9,20 @@ from PIL import Image
|
|||
def printHelp():
|
||||
print("Convert image to C array for small3dlib.")
|
||||
print("usage:\n")
|
||||
print(" python img2array.py [TODO] file\n")
|
||||
print(" TODO\n")
|
||||
print(" python img2array.py [-xW -yH -h -nS -pT -5] file\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("by Miloslav \"drummyfish\" Ciz")
|
||||
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:
|
||||
printHelp()
|
||||
quit()
|
||||
|
@ -26,6 +34,7 @@ NAME = "texture"
|
|||
GUARDS = False
|
||||
OUT_WIDTH = 64
|
||||
OUT_HEIGHT = 64
|
||||
USE_565 = False
|
||||
|
||||
for s in sys.argv:
|
||||
if s [:2] == "-x":
|
||||
|
@ -36,6 +45,8 @@ for s in sys.argv:
|
|||
GUARDS = True
|
||||
elif s[:2] == "-n":
|
||||
NAME = s[2:]
|
||||
elif s[:2] == "-5":
|
||||
USE_565 = True
|
||||
elif s[:2] == "-p":
|
||||
PALETTE = s[2:]
|
||||
USE_PALETTE = True
|
||||
|
@ -49,7 +60,7 @@ paletteArray = []
|
|||
image = Image.open(FILENAME).convert("RGB")
|
||||
pixels = image.load()
|
||||
|
||||
if len(PALETTE) > 0:
|
||||
if USE_PALETTE > 0:
|
||||
palette = Image.open(PALETTE).convert("RGB")
|
||||
pixelsPal = palette.load()
|
||||
|
||||
|
@ -57,9 +68,13 @@ if len(PALETTE) > 0:
|
|||
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])
|
||||
|
||||
if USE_565:
|
||||
paletteArray.append(rgbTo565(c))
|
||||
else:
|
||||
paletteArray.append(c[0])
|
||||
paletteArray.append(c[1])
|
||||
paletteArray.append(c[2])
|
||||
|
||||
image2 = Image.new("RGB",(OUT_WIDTH,OUT_HEIGHT),color="white")
|
||||
pixels2 = image2.load()
|
||||
|
@ -89,9 +104,13 @@ for y in range(OUT_HEIGHT):
|
|||
imageArray.append(closestIndex)
|
||||
pixels2[x,y] = paletteColors[closestIndex]
|
||||
else:
|
||||
imageArray.append(pixel[0])
|
||||
imageArray.append(pixel[1])
|
||||
imageArray.append(pixel[2])
|
||||
if USE_565:
|
||||
imageArray.append(rgbTo565(pixel))
|
||||
else:
|
||||
imageArray.append(pixel[0])
|
||||
imageArray.append(pixel[1])
|
||||
imageArray.append(pixel[2])
|
||||
|
||||
pixels2[x,y] = pixel
|
||||
|
||||
#-----------------------
|
||||
|
@ -109,12 +128,12 @@ def printArray(array, name, sizeString):
|
|||
|
||||
if lineLen > 80:
|
||||
arrayString += "\n"
|
||||
lineLen -= 80
|
||||
lineLen = 0
|
||||
|
||||
arrayString += item
|
||||
|
||||
print(arrayString[:-1])
|
||||
print("}; // " + name + "\n")
|
||||
print("}; // " + name)
|
||||
|
||||
if GUARDS:
|
||||
print("#ifndef " + NAME.upper() + "_TEXTURE_H")
|
||||
|
@ -122,12 +141,13 @@ if GUARDS:
|
|||
|
||||
if USE_PALETTE:
|
||||
printArray(paletteArray,NAME + "Palette",str(len(paletteArray)))
|
||||
print("")
|
||||
|
||||
print("#define " + NAME.upper() + "_TEXTURE_WIDTH " + str(OUT_WIDTH))
|
||||
print("#define " + NAME.upper() + "_TEXTURE_HEIGHT " + str(OUT_HEIGHT))
|
||||
print("")
|
||||
|
||||
printArray(imageArray,NAME + "Texture",NAME.upper() + "_TEXTURE_WIDTH * " + NAME.upper() + "_TEXTURE_HEIGHT * 3")
|
||||
printArray(imageArray,NAME + "Texture",str(len(imageArray)))
|
||||
|
||||
if GUARDS:
|
||||
print("\n#endif // guard")
|
||||
|
|
Loading…
Reference in a new issue