Loading

palette-converter_v4

  1. import math
  2. import os
  3. from os import remove
  4. from PIL import Image
  5.  
  6. #starting message
  7. print("-"*79)
  8. print("Starting...")
  9.  
  10. #path definition
  11. diskPath = "E:/_BRIX/_BRIX-repository/scripts/"#"C:/Users/Pavel/Downloads/scripts/"
  12. inputFolder = diskPath + "input/"
  13. outputFolder = diskPath + "output/"
  14.  
  15. #printing path just to check
  16. print("inputFolder is " + inputFolder)
  17. print("outputFolder is " + outputFolder)
  18.  
  19. #open palette image
  20. palette = Image.open(inputFolder + "openttd-palette-dos.png")
  21. print("Opening: " + "openttd-palette-dos.png")  
  22.  
  23. p=[]
  24.  
  25. for b in range(0,palette.height):
  26.   for a in range(0, palette.width):
  27.     p.append(palette.getpixel((a,b)))
  28.    
  29. #print(p)
  30.  
  31. def rgb2palette(inputImage, frameNumber):
  32.   #open input image
  33.   i = Image.open(inputFolder + inputImage + frameNumber + ".png")
  34.   print("Opening: " + inputImage + frameNumber + ".png")  
  35.  
  36.   #create new empty image for output
  37.   imageOutput = Image.new("RGBA", (i.width,i.height), color=(0,0,0,0))
  38.    
  39.  
  40.  
  41.   for y in range (0, i.height):
  42.     for x in range (0, i.width):
  43.       winnerDistance = 100000000
  44.       winnerID = 0
  45.      
  46.       pixelNumber = x + (y * i.width)
  47.       pix = i.getpixel((x,y))
  48.       pixRed = pix[0]
  49.       pixGreen = pix[1]
  50.       pixBlue = pix[2]
  51.       pixAlpha = pix[3]
  52.            
  53.       #if alpha is to be cleared, set to 0 and skip palette checking
  54.       if pixAlpha < 128:
  55.         finalAlpha = 0
  56.         colorOffset = 0
  57.       if pixAlpha >= 128 and pixAlpha < 178:
  58.         finalAlpha = 255
  59.         colorOffset = 1
  60.       if pixAlpha >= 178 and pixAlpha < 230:
  61.         finalAlpha = 255
  62.         colorOffset = 2
  63.       if pixAlpha >= 230:
  64.         finalAlpha = 255
  65.         colorOffset = 0
  66.      
  67.       if pixAlpha >= 128:
  68.         for z in range(0, 255):
  69.           colorArray = p[z]
  70.           #print("current z is ..." + str(colorArray) )
  71.           paletteR = colorArray[0]
  72.           paletteG = colorArray[1]
  73.           paletteB = colorArray[2]
  74.           distance = math.sqrt( ((pixRed-paletteR)*(pixRed-paletteR)) + ((pixGreen-paletteG)*(pixGreen-paletteG)) + ((pixBlue-paletteB)*(pixBlue-paletteB) ))
  75.           #print("Pixel " + str(pixelNumber) + ": " + str(distance) + " ,Alpha: " + str(pixAlpha) )
  76.          
  77.           #taking the distance and comparing it to previous "round"
  78.           temporaryDistance = distance
  79.           temporaryID = z
  80.           #print("temporaryID is ..." + str(temporaryID) )
  81.           if temporaryDistance < winnerDistance:
  82.             winnerDistance = temporaryDistance
  83.             winnerID = temporaryID
  84.             #print("new winner is ..." + str(winnerID) + "!!!")
  85.    
  86.       #colorOffset changes based on Value of the input
  87.      
  88.       if pixRed >= pixGreen and pixRed >= pixBlue:
  89.         highestValue = pixRed
  90.       if pixGreen >= pixRed and pixGreen >= pixBlue:
  91.         highestValue = pixGreen
  92.       if pixBlue >= pixRed and pixBlue >= pixGreen:
  93.         highestValue = pixBlue    
  94.      
  95.       if highestValue < 128:
  96.         negation = -1
  97.         colorOffset = colorOffset * negation
  98.      
  99.       print("colorOffset is ... " + str(colorOffset) )
  100.       finalColor = p[winnerID - colorOffset]
  101.      
  102.       finalR = finalColor[0]
  103.       finalG = finalColor[1]
  104.       finalB = finalColor[2]
  105.       #finalAlpha taken from the if output above
  106.      
  107.       imageOutput.putpixel((x,y),(finalR,finalG,finalB,finalAlpha))
  108.       print("Pixel " + str(pixelNumber) + ": R= " + str(finalR) + ", G= " + str(finalG) + ", B= " + str(finalB) + ", A= "  + str(finalAlpha)   )
  109.          
  110.      
  111.   os.makedirs(outputFolder, exist_ok = True)
  112.   imageOutput.save(outputFolder + inputImage + "_8bpp.png")
  113.      
  114.      
  115. def run():
  116.   rgb2palette("BRIDGES_", "0000")
  117.  
  118.  
  119.  
  120. import traceback
  121.  
  122. try:
  123.  
  124.   run()
  125.  
  126. except Exception as e:
  127.  
  128.   traceback.print_exc()
  129.  
  130. input("Press enter to continue...")

Comments