Loading

Palette-converter-v-1-7

  1. import math
  2. import time
  3. import datetime
  4. import os
  5. from os import remove
  6. from PIL import Image
  7.  
  8. #starting message
  9. print("-"*79)
  10. print("Starting...")
  11.  
  12. #path definition
  13. diskPath = "C:/Users/Pavel/Downloads/scripts/"#"E:/_BRIX/_BRIX-repository/scripts/"
  14. inputFolder = diskPath + "input/"
  15. outputFolder = diskPath + "output/"
  16.  
  17. #printing path just to check
  18. print("inputFolder is " + inputFolder)
  19. print("outputFolder is " + outputFolder)
  20.  
  21. #started time
  22. tt = time.time()
  23. startedTime = datetime.datetime.fromtimestamp(tt).strftime('%H:%M:%S')
  24.  
  25.  
  26.  
  27. #open palette image
  28. palette = Image.open(inputFolder + "openttd-palette-dos.png")
  29. print("Opening: " + "openttd-palette-dos.png")  
  30.  
  31. p=[]
  32.  
  33. for b in range(0,palette.height):
  34.   for a in range(0, palette.width):
  35.     p.append(palette.getpixel((a,b)))
  36.    
  37. #print(p)
  38.  
  39. def rgb2palette(inputImage, frameNumber):
  40.   #open input image
  41.   i = Image.open(inputFolder + inputImage + frameNumber + ".png")
  42.   print("Opening: " + inputImage + frameNumber + ".png")  
  43.  
  44.   #create new empty image for output
  45.   imageOutput = Image.new("RGBA", (i.width,i.height), color=(0,0,0,0))
  46.    
  47.   for y in range (0, i.height):
  48.     #timeStamp
  49.     ts = time.time()
  50.     timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S')
  51.     print(timeStamp + " - " + inputImage + " row {}".format(y))
  52.     for x in range (0, i.width):
  53.       #defining winner variables
  54.       winnerDistance = 100000000
  55.       winnerID = 0
  56.      
  57.       #loading pixel from image and separating RGBA
  58.       pixelNumber = x + (y * i.width)
  59.       pix = i.getpixel((x,y))
  60.       pixRed = pix[0]
  61.       pixGreen = pix[1]
  62.       pixBlue = pix[2]
  63.       pixAlpha = pix[3]
  64.            
  65.       #check Alpha in pixel, and output alpha/color offset
  66.       if pixAlpha < 128:
  67.         finalAlpha = 0
  68.         colorOffset = 0
  69.       if pixAlpha >= 128 and pixAlpha < 178:
  70.         finalAlpha = 255
  71.         colorOffset = 1
  72.       if pixAlpha >= 178 and pixAlpha < 230:
  73.         finalAlpha = 255
  74.         colorOffset = 2
  75.       if pixAlpha >= 230:
  76.         finalAlpha = 255
  77.         colorOffset = 0
  78.      
  79.       #if alpha above 50%, do colour comparing to palette
  80.       if pixAlpha >= 128:
  81.         for z, (cr, cg, cb, ca) in enumerate(p):
  82.           dr = pixRed - cr
  83.           dg = pixGreen - cg
  84.           db = pixBlue - cb
  85.           distance = dr*dr + dg*dg + db*db
  86.  
  87.           if distance < winnerDistance:
  88.             winnerDistance = distance
  89.             winnerID = z
  90.    
  91.       #compare input RGB channels and output highest value
  92.       if pixRed >= pixGreen and pixRed >= pixBlue:
  93.         highestValue = pixRed
  94.       if pixGreen >= pixRed and pixGreen >= pixBlue:
  95.         highestValue = pixGreen
  96.       if pixBlue >= pixRed and pixBlue >= pixGreen:
  97.         highestValue = pixBlue    
  98.       # set color offset +/- based on colour value
  99.       if highestValue < 128:
  100.         negation = -1
  101.         colorOffset = colorOffset * negation
  102.       #print("colorOffset is ... " + str(colorOffset) )
  103.      
  104.       #final color changed by colorOffset
  105.       finalColor = p[winnerID - colorOffset]
  106.      
  107.       finalR = finalColor[0]
  108.       finalG = finalColor[1]
  109.       finalB = finalColor[2]
  110.       #finalAlpha taken from the if output above palette colour comparing
  111.      
  112.       #put the final pixel into the output picture
  113.       imageOutput.putpixel((x,y),(finalR,finalG,finalB,finalAlpha))
  114.       #print("Pixel " + str(pixelNumber) + ": R= " + str(finalR) + ", G= " + str(finalG) + ", B= " + str(finalB) + ", A= "  + str(finalAlpha)   )
  115.          
  116.      
  117.   os.makedirs(outputFolder, exist_ok = True)
  118.   imageOutput.save(outputFolder + inputImage + "_8bpp.png")
  119.  
  120.   #finished time
  121.   tx = time.time()
  122.   finishedTime = datetime.datetime.fromtimestamp(tx).strftime('%H:%M:%S')
  123.  
  124.   print("Started:  " + startedTime)
  125.   print("Finished: " + finishedTime)
  126.  
  127.  
  128.      
  129.      
  130. def run():
  131.   rgb2palette("TRACKS_EDIT", "")
  132.   rgb2palette("BRIDGES_", "0000")
  133.   rgb2palette("LAND_OUTPUT_", "0000")
  134.   rgb2palette("ROADS_OUTPUT_", "0000")
  135.   rgb2palette("TREES_OUTPUT_", "0000")
  136.   rgb2palette("SIGNALS-01_", "0000")
  137.   rgb2palette("SIGNALS-02_", "0000")
  138.  
  139.  
  140.  
  141. import traceback
  142.  
  143. try:
  144.  
  145.   run()
  146.  
  147. except Exception as e:
  148.  
  149.   traceback.print_exc()
  150.  
  151. input("Press enter to continue...")

Comments