Loading

sdsdasdsd

  1. import math
  2. import time
  3. import datetime
  4. import os
  5. from os import remove
  6. from PIL import Image
  7. from multiprocessing import Pool
  8. import multiprocessing
  9.  
  10. #starting message
  11. print("-"*79)
  12. print("Starting...")
  13.  
  14. #path definition
  15. diskPath = 'D:/OpenTTD_repositories/BRIX/scripts/' #"C:/Users/Pavel/Downloads/scripts/"#"E:/_BRIX/_BRIX-repository/scripts/"
  16. inputFolder = diskPath + "input/"
  17. outputFolder = diskPath + "output/"
  18.  
  19. #printing path just to check
  20. print("inputFolder is " + inputFolder)
  21. print("outputFolder is " + outputFolder)
  22.  
  23. #started time
  24. tt = time.time()
  25. startedTime = datetime.datetime.fromtimestamp(tt).strftime('%H:%M:%S')
  26.  
  27.  
  28.  
  29. #open palette image
  30. palette = Image.open(inputFolder + "openttd-palette-dos.png")
  31. print("Opening and loading palette: " + "openttd-palette-dos.png")  
  32.  
  33. p=[]
  34.  
  35. for b in range(0,palette.height):
  36.   for a in range(0, palette.width):
  37.     p.append(palette.getpixel((a,b)))
  38.    
  39. print('Palette loaded.')
  40.  
  41. # ----------------------------------------------------------------------------------------------------------------
  42. # ----------------------------------------------------------------------------------------------------------------
  43. # ----------------------------------------------------------------------------------------------------------------
  44.  
  45.  
  46. def rgb2palette(args):
  47.   input_image = args[0]
  48.   x_start = args[1]
  49.   x_end = args[2]
  50.   #open input image
  51.   i = Image.open(inputFolder + input_image + ".png")
  52.   print("Opening: " + input_image + ".png")  
  53.  
  54.   #create new empty image for output
  55.   imageOutput = Image.new("RGBA", (i.width,i.height), color=(0,0,0,0))
  56.  
  57.   for y in range (0, i.height):
  58.     #timeStamp
  59.     ts = time.time()
  60.     timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S')
  61.     #print(timeStamp + " - " + input_image + " row {}".format(y))
  62.     for x in range (x_start, x_end):
  63.       print(timeStamp + " - " + input_image + " row {}".format(x))
  64.       #defining winner variables
  65.       winnerDistance = 100000000
  66.       winnerID = 0
  67.      
  68.       #loading pixel from image and separating RGBA
  69.       pixelNumber = x + (y * i.width)
  70.       pix = i.getpixel((x,y))
  71.       pixRed = pix[0]
  72.       pixGreen = pix[1]
  73.       pixBlue = pix[2]
  74.       pixAlpha = pix[3]
  75.            
  76.       #check Alpha in pixel, and output alpha/color offset
  77.       if pixAlpha < 128:
  78.         finalAlpha = 0
  79.         colorOffset = 0
  80.       if pixAlpha >= 128 and pixAlpha < 178:
  81.         finalAlpha = 255
  82.         colorOffset = 1
  83.       if pixAlpha >= 178 and pixAlpha < 230:
  84.         finalAlpha = 255
  85.         colorOffset = 2
  86.       if pixAlpha >= 230:
  87.         finalAlpha = 255
  88.         colorOffset = 0
  89.      
  90.       #if alpha above 50%, do colour comparing to palette
  91.       if pixAlpha >= 128:
  92.         for z, (r1, g1, b1, ca) in enumerate(p):
  93.           #dr = pixRed - cr
  94.           #dg = pixGreen - cg
  95.           #db = pixBlue - cb
  96.           #distance = dr*dr + dg*dg + db*db
  97.           l1 = (r1*299 + g1*587 + b1*114) / 255000
  98.           l2 = (pixRed*299 + pixGreen*587 + pixBlue*114) / 255000
  99.           dL = l1-l2
  100.           dR = (r1-pixRed)/255
  101.           dG = (g1-pixGreen)/255
  102.           dB = (b1-pixBlue)/255
  103.           distance = (dR*dR*0.299 + dG*dG*0.587 + dB*dB*0.114)*0.75 + dL*dL
  104.          
  105.           if distance < winnerDistance:
  106.             winnerDistance = distance
  107.             winnerID = z
  108.    
  109.       #compare input RGB channels and output highest value
  110.       if pixRed >= pixGreen and pixRed >= pixBlue:
  111.         highestValue = pixRed
  112.       if pixGreen >= pixRed and pixGreen >= pixBlue:
  113.         highestValue = pixGreen
  114.       if pixBlue >= pixRed and pixBlue >= pixGreen:
  115.         highestValue = pixBlue    
  116.       # set color offset +/- based on colour value
  117.       if highestValue < 128:
  118.         negation = -1
  119.         colorOffset = colorOffset * negation
  120.       #print("colorOffset is ... " + str(colorOffset) )
  121.      
  122.       #final color changed by colorOffset
  123.       finalColor = p[winnerID - colorOffset]
  124.      
  125.       finalR = finalColor[0]
  126.       finalG = finalColor[1]
  127.       finalB = finalColor[2]
  128.       #finalAlpha taken from the if output above palette colour comparing
  129.      
  130.  
  131.       #put the final pixel into the output picture
  132.       imageOutput.putpixel((x,y),(finalR,finalG,finalB,finalAlpha))
  133.       #print("Pixel " + str(pixelNumber) + ": R= " + str(finalR) + ", G= " + str(finalG) + ", B= " + str(finalB) + ", A= "  + str(finalAlpha)   )
  134.       os.makedirs(outputFolder, exist_ok = True)
  135.    
  136.   print('Saving picture to Output')
  137.   imageOutput.save(outputFolder + input_image[-5:] + "WTF_TESTING_8bpp.png") #assumes _#### frame number format (removes last 5 characters)
  138.          
  139.      
  140.  
  141.      
  142.      
  143. def run():
  144.   # ----------------------------------------------------------------------------------------------------------------
  145.   #VARIABLES
  146.   # ----------------------------------------------------------------------------------------------------------------
  147.   job_list = [
  148.       'test0'
  149.       #'BRIDGES_0000'
  150.       #'LAND_OUTPUT_0000'
  151.       ]
  152.   # ----------------------------------------------------------------------------------------------------------------
  153.   thread_count = 16
  154.   # ----------------------------------------------------------------------------------------------------------------
  155.   # ----------------------------------------------------------------------------------------------------------------
  156.   # ----------------------------------------------------------------------------------------------------------------
  157.  
  158.   for job in job_list:
  159.     all_jobs = []
  160.     job_chunks = []
  161.     queue = []
  162.  
  163.     extra = 0
  164.     chunk_image = Image.open(inputFolder + job + ".png")
  165.     chunk_average_size = math.floor(chunk_image.width/thread_count)
  166.     chunk_modulo_size = chunk_image.width % thread_count
  167.     for thread in range(0, thread_count):
  168.       start = chunk_average_size * thread
  169.       end = (chunk_average_size * (thread+1)) -1
  170.  
  171.       start += extra
  172.       if thread < chunk_modulo_size:  
  173.         extra += 1
  174.       end += extra
  175.  
  176.       start_and_end = [job, start, end]
  177.      
  178.       job_chunks.append(start_and_end)
  179.  
  180.       queue.append( [job, start, end] )
  181.       #print(str(queue))
  182.  
  183.     all_jobs.append(job_chunks)
  184.  
  185.     for a_job in all_jobs:
  186.       #print('-'*32)
  187.       thread_id = 0
  188.       for b_thread in a_job:
  189.         if thread_id == 0:
  190.           thread_id += 1
  191.           print('Job: ' + str(b_thread[0]))#
  192.           print(' '*10 + 'Start, ' + 'End')#
  193.           print('Thread ' + str(thread_id) + ': ' + str(b_thread[1]) + ', ' + str(b_thread[2]))#
  194.        
  195.        
  196.    
  197.     #print(all_jobs)
  198.    
  199.     # ----------------------------------------------------------------------------------------------------------------
  200.     # PROCESS
  201.     # ----------------------------------------------------------------------------------------------------------------
  202.    
  203.     pool = multiprocessing.Pool(processes = thread_count)
  204.     for q in queue:
  205.       pool.map(rgb2palette, queue)
  206.     pool.close()
  207.     pool.join()
  208.  
  209.    
  210.     #imageOutput.save(outputFolder + input_image[-5:] + "_8bpp.png") #assumes _#### frame number format (removes last 5 characters)
  211.     #print('SAVING IMAGE')
  212.  
  213.    
  214.     #finished time
  215.     tx = time.time()
  216.     finishedTime = datetime.datetime.fromtimestamp(tx).strftime('%H:%M:%S')
  217.    
  218.     print("Started:  " + startedTime)
  219.     print("Finished: " + finishedTime)
  220.    
  221.  
  222. #run()
  223.  
  224. if __name__ == '__main__':
  225.   #freeze_support()
  226.   run()

Comments