import math import os from os import remove from PIL import Image import palette #starting message print("-"*79) print("Starting...") #path definition #diskPath = "E:/_BRIX/_BRIX-repository/scripts/"#"C:/Users/Pavel/Downloads/scripts/" diskPath = "./" 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) for i in range(256): r = palette.dos_palette[i*3] g = palette.dos_palette[i*3 + 1] b = palette.dos_palette[i*3 + 2] p.append((r, g, b)) 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): print("row {}".format(y)) 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 elif pixAlpha < 178: finalAlpha = 255 colorOffset = 1 elif pixAlpha < 230: finalAlpha = 255 colorOffset = 2 else: finalAlpha = 255 colorOffset = 0 if pixAlpha >= 128: for z, (cr, cg, cb) in enumerate(p): #print("current z is ..." + str(colorArray) ) dr = pixRed - cr dg = pixGreen - cg db = pixBlue - cb distance = dr*dr + dg*dg + db*db #print("Pixel " + str(pixelNumber) + ": " + str(distance) + " ,Alpha: " + str(pixAlpha) ) #taking the distance and comparing it to previous "round" if distance < winnerDistance: winnerDistance = distance winnerID = z #print("new winner is ..." + str(winnerID) + "!!!") #colorOffset changes based on Value of the input if pixRed >= pixGreen and pixRed >= pixBlue: highestValue = pixRed elif pixGreen >= pixRed and pixGreen >= pixBlue: highestValue = pixGreen else: 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") rgb2palette("logo_", "0000") import traceback try: run() except Exception as e: traceback.print_exc() #input("Press enter to continue...")