Loading

palette-converter_v5

  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.       #defining winner variables
  44.       winnerDistance = 100000000
  45.       winnerID = 0
  46.      
  47.       #loading pixel from image and separating RGBA
  48.       pixelNumber = x + (y * i.width)
  49.       pix = i.getpixel((x,y))
  50.       pixRed = pix[0]
  51.       pixGreen = pix[1]
  52.       pixBlue = pix[2]
  53.       pixAlpha = pix[3]
  54.            
  55.       #check Alpha in pixel, and output alpha/color offset
  56.       if pixAlpha < 128:
  57.         finalAlpha = 0
  58.         colorOffset = 0
  59.       if pixAlpha >= 128 and pixAlpha < 178:
  60.         finalAlpha = 255
  61.         colorOffset = 1
  62.       if pixAlpha >= 178 and pixAlpha < 230:
  63.         finalAlpha = 255
  64.         colorOffset = 2
  65.       if pixAlpha >= 230:
  66.         finalAlpha = 255
  67.         colorOffset = 0
  68.      
  69.       #if alpha above 50%, do colour comparing to palette
  70.       if pixAlpha >= 128:
  71.         for z in range(0, 255):
  72.           #taking a palette colour
  73.           colorArray = p[z]
  74.           paletteR = colorArray[0]
  75.           paletteG = colorArray[1]
  76.           paletteB = colorArray[2]
  77.           #calculating distance between palette colour and input colour
  78.           distance = math.sqrt( ((pixRed-paletteR)*(pixRed-paletteR)) + ((pixGreen-paletteG)*(pixGreen-paletteG)) + ((pixBlue-paletteB)*(pixBlue-paletteB) ))
  79.           #print("Pixel " + str(pixelNumber) + ": " + str(distance) + " ,Alpha: " + str(pixAlpha) )
  80.          
  81.           #taking the distance and comparing it to previous "round"
  82.           temporaryDistance = distance
  83.           temporaryID = z
  84.           #print("temporaryID is ..." + str(temporaryID) )
  85.          
  86.           #declare new winner if it's shorter distance
  87.           if temporaryDistance < winnerDistance:
  88.             winnerDistance = temporaryDistance
  89.             winnerID = temporaryID
  90.             #print("new winner is ..." + str(winnerID) + "!!!")
  91.    
  92.       #compare input RGB channels and output highest value
  93.       if pixRed >= pixGreen and pixRed >= pixBlue:
  94.         highestValue = pixRed
  95.       if pixGreen >= pixRed and pixGreen >= pixBlue:
  96.         highestValue = pixGreen
  97.       if pixBlue >= pixRed and pixBlue >= pixGreen:
  98.         highestValue = pixBlue    
  99.       # set color offset +/- based on colour value
  100.       if highestValue < 128:
  101.         negation = -1
  102.         colorOffset = colorOffset * negation
  103.       #print("colorOffset is ... " + str(colorOffset) )
  104.      
  105.       #final color changed by colorOffset
  106.       finalColor = p[winnerID - colorOffset]
  107.      
  108.       finalR = finalColor[0]
  109.       finalG = finalColor[1]
  110.       finalB = finalColor[2]
  111.       #finalAlpha taken from the if output above palette colour comparing
  112.      
  113.       #put the final pixel into the output picture
  114.       imageOutput.putpixel((x,y),(finalR,finalG,finalB,finalAlpha))
  115.       #print("Pixel " + str(pixelNumber) + ": R= " + str(finalR) + ", G= " + str(finalG) + ", B= " + str(finalB) + ", A= "  + str(finalAlpha)   )
  116.          
  117.      
  118.   os.makedirs(outputFolder, exist_ok = True)
  119.   imageOutput.save(outputFolder + inputImage + "_8bpp.png")
  120.      
  121.      
  122. def run():
  123.   rgb2palette("BRIDGES_", "0000")
  124.  
  125.  
  126.  
  127. import traceback
  128.  
  129. try:
  130.  
  131.   run()
  132.  
  133. except Exception as e:
  134.  
  135.   traceback.print_exc()
  136.  
  137. input("Press enter to continue...")

Comments