Loading

Paste #psgnsq161

  1. import math
  2. import os
  3. from os import remove
  4. from PIL import Image
  5.  
  6. import palette
  7.  
  8. #starting message
  9. print("-"*79)
  10. print("Starting...")
  11.  
  12. #path definition
  13. #diskPath = "E:/_BRIX/_BRIX-repository/scripts/"#"C:/Users/Pavel/Downloads/scripts/"
  14. diskPath = "./"
  15. inputFolder = diskPath + "input/"
  16. outputFolder = diskPath + "output/"
  17.  
  18. #printing path just to check
  19. print("inputFolder is " + inputFolder)
  20. print("outputFolder is " + outputFolder)
  21.  
  22. ##open palette image
  23. #palette = Image.open(inputFolder + "openttd-palette-dos.png")
  24. #print("Opening: " + "openttd-palette-dos.png")
  25.  
  26. p=[]
  27.  
  28. #for b in range(0,palette.height):
  29. #  for a in range(0, palette.width):
  30. #    p.append(palette.getpixel((a,b)))
  31. #
  32. ##print(p)
  33.  
  34. for i in range(256):
  35.     r = palette.dos_palette[i*3]
  36.     g = palette.dos_palette[i*3 + 1]
  37.     b = palette.dos_palette[i*3 + 2]
  38.     p.append((r, g, b))
  39.  
  40. def rgb2palette(inputImage, frameNumber):
  41.   #open input image
  42.   i = Image.open(inputFolder + inputImage + frameNumber + ".png")
  43.   print("Opening: " + inputImage + frameNumber + ".png")
  44.  
  45.   #create new empty image for output
  46.   imageOutput = Image.new("RGBA", (i.width,i.height), color=(0,0,0,0))
  47.  
  48.  
  49.  
  50.   for y in range (0, i.height):
  51.     print("row {}".format(y))
  52.     for x in range (0, i.width):
  53.       winnerDistance = 100000000
  54.       winnerID = 0
  55.  
  56.       #pixelNumber = x + (y * i.width)
  57.       pix = i.getpixel((x,y))
  58.       pixRed = pix[0]
  59.       pixGreen = pix[1]
  60.       pixBlue = pix[2]
  61.       pixAlpha = pix[3]
  62.  
  63.       #if alpha is to be cleared, set to 0 and skip palette checking
  64.       if pixAlpha < 128:
  65.         finalAlpha = 0
  66.         colorOffset = 0
  67.       elif pixAlpha < 178:
  68.         finalAlpha = 255
  69.         colorOffset = 1
  70.       elif pixAlpha < 230:
  71.         finalAlpha = 255
  72.         colorOffset = 2
  73.       else:
  74.         finalAlpha = 255
  75.         colorOffset = 0
  76.  
  77.       if pixAlpha >= 128:
  78.         for z, (cr, cg, cb) in enumerate(p):
  79.           #print("current z is ..." + str(colorArray) )
  80.           dr = pixRed - cr
  81.           dg = pixGreen - cg
  82.           db = pixBlue - cb
  83.           distance = dr*dr + dg*dg + db*db
  84.           #print("Pixel " + str(pixelNumber) + ": " + str(distance) + " ,Alpha: " + str(pixAlpha) )
  85.  
  86.           #taking the distance and comparing it to previous "round"
  87.           if distance < winnerDistance:
  88.             winnerDistance = distance
  89.             winnerID = z
  90.             #print("new winner is ..." + str(winnerID) + "!!!")
  91.  
  92.       #colorOffset changes based on Value of the input
  93.  
  94.       if pixRed >= pixGreen and pixRed >= pixBlue:
  95.         highestValue = pixRed
  96.       elif pixGreen >= pixRed and pixGreen >= pixBlue:
  97.         highestValue = pixGreen
  98.       else:
  99.         highestValue = pixBlue
  100.  
  101.       if highestValue < 128:
  102.         negation = -1
  103.         colorOffset = colorOffset * negation
  104.  
  105.       #print("colorOffset is ... " + str(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
  112.  
  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.  
  121. def run():
  122.   #rgb2palette("BRIDGES_", "0000")
  123.   rgb2palette("logo_", "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