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):
#defining winner variables
winnerDistance = 100000000
winnerID = 0
#loading pixel from image and separating RGBA
pixelNumber = x + (y * i.width)
pix = i.getpixel((x,y))
pixRed = pix[0]
pixGreen = pix[1]
pixBlue = pix[2]
pixAlpha = pix[3]
#check Alpha in pixel, and output alpha/color offset
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 alpha above 50%, do colour comparing to palette
if pixAlpha >= 128:
for z in range(0, 255):
#taking a palette colour
colorArray = p[z]
paletteR = colorArray[0]
paletteG = colorArray[1]
paletteB = colorArray[2]
#calculating distance between palette colour and input colour
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) )
#declare new winner if it's shorter distance
if temporaryDistance < winnerDistance:
winnerDistance = temporaryDistance
winnerID = temporaryID
#print("new winner is ..." + str(winnerID) + "!!!")
#compare input RGB channels and output highest value
if pixRed >= pixGreen and pixRed >= pixBlue:
highestValue = pixRed
if pixGreen >= pixRed and pixGreen >= pixBlue:
highestValue = pixGreen
if pixBlue >= pixRed and pixBlue >= pixGreen:
highestValue = pixBlue
# set color offset +/- based on colour value
if highestValue < 128:
negation = -1
colorOffset = colorOffset * negation
#print("colorOffset is ... " + str(colorOffset) )
#final color changed by colorOffset
finalColor = p[winnerID - colorOffset]
finalR = finalColor[0]
finalG = finalColor[1]
finalB = finalColor[2]
#finalAlpha taken from the if output above palette colour comparing
#put the final pixel into the output picture
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...")