import math import os from os import remove from PIL import Image #starting message print("-"*79) print("Starting...") #path definition diskPath = "E:/_BRIX/_BRIX-repository/scripts/"#"C:/Users/Pavel/Downloads/scripts/" inputFolder = diskPath + "input/" outputFolder = diskPath + "output/" #printing path just to check print("inputFolder is " + inputFolder) print("outputFolder is " + outputFolder) #open palette image palette = Image.open(inputFolder + "openttd-palette-dos.png") print("Opening: " + "openttd-palette-dos.png") p=[] for b in range(0,palette.height): for a in range(0, palette.width): p.append(palette.getpixel((a,b))) #print(p) def rgb2palette(inputImage, frameNumber): #open input image i = Image.open(inputFolder + inputImage + frameNumber + ".png") print("Opening: " + inputImage + frameNumber + ".png") #create new empty image for output imageOutput = Image.new("RGBA", (i.width,i.height), color=(0,0,0,0)) for y in range (0, i.height): for x in range (0, i.width): winnerDistance = 100000000 winnerID = 0 pixelNumber = x + (y * i.width) pix = i.getpixel((x,y)) pixRed = pix[0] pixGreen = pix[1] pixBlue = pix[2] pixAlpha = pix[3] #if alpha is to be cleared, set to 0 and skip palette checking if pixAlpha < 128: finalAlpha = 0 colorOffset = 0 if pixAlpha >= 128 and pixAlpha < 178: finalAlpha = 255 colorOffset = 1 if pixAlpha >= 178 and pixAlpha < 230: finalAlpha = 255 colorOffset = 2 if pixAlpha >= 230: finalAlpha = 255 colorOffset = 0 if pixAlpha >= 128: for z in range(0, 255): colorArray = p[z] #print("current z is ..." + str(colorArray) ) paletteR = colorArray[0] paletteG = colorArray[1] paletteB = colorArray[2] distance = math.sqrt( ((pixRed-paletteR)*(pixRed-paletteR)) + ((pixGreen-paletteG)*(pixGreen-paletteG)) + ((pixBlue-paletteB)*(pixBlue-paletteB) )) #print("Pixel " + str(pixelNumber) + ": " + str(distance) + " ,Alpha: " + str(pixAlpha) ) #taking the distance and comparing it to previous "round" temporaryDistance = distance temporaryID = z #print("temporaryID is ..." + str(temporaryID) ) if temporaryDistance < winnerDistance: winnerDistance = temporaryDistance winnerID = temporaryID #print("new winner is ..." + str(winnerID) + "!!!") #colorOffset changes based on Value of the input if pixRed >= pixGreen and pixRed >= pixBlue: highestValue = pixRed if pixGreen >= pixRed and pixGreen >= pixBlue: highestValue = pixGreen if pixBlue >= pixRed and pixBlue >= pixGreen: highestValue = pixBlue if highestValue < 128: negation = -1 colorOffset = colorOffset * negation print("colorOffset is ... " + str(colorOffset) ) finalColor = p[winnerID - colorOffset] finalR = finalColor[0] finalG = finalColor[1] finalB = finalColor[2] #finalAlpha taken from the if output above imageOutput.putpixel((x,y),(finalR,finalG,finalB,finalAlpha)) print("Pixel " + str(pixelNumber) + ": R= " + str(finalR) + ", G= " + str(finalG) + ", B= " + str(finalB) + ", A= " + str(finalAlpha) ) os.makedirs(outputFolder, exist_ok = True) imageOutput.save(outputFolder + inputImage + "_8bpp.png") def run(): rgb2palette("BRIDGES_", "0000") import traceback try: run() except Exception as e: traceback.print_exc() input("Press enter to continue...")