Loading

palette-converter-v18

  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. from copy import deepcopy
  10. import argparse
  11.  
  12. def print_start(bool, string):
  13.   if bool == True:
  14.     print(str(string))
  15. def print_lvl_0(debug_level, string):
  16.   print(str(string))
  17. def print_lvl_1(debug_level, string):
  18.   if debug_level > 0:
  19.     print(str(string))
  20. def print_lvl_2(debug_level, string):
  21.   if debug_level > 1:
  22.     print(str(string))
  23. def print_lvl_3(debug_level, string):
  24.   if debug_level > 2:
  25.     print(str(string))
  26. def print_lvl_4(debug_level, string):
  27.   if debug_level > 3:
  28.     print(str(string))
  29. def print_lvl_5(debug_level, string):
  30.   if debug_level > 4:
  31.     print(str(string))
  32. def print_lvl_6(debug_level, string):
  33.   if debug_level > 5:
  34.     print(str(string))
  35. def print_lvl_7(debug_level, string):
  36.   if debug_level > 6:
  37.     print(str(string))
  38.  
  39.  
  40. show_starting_messages = False
  41. #starting message
  42. print_start(show_starting_messages, '-'*79)
  43. print_start(show_starting_messages, 'Starting...')
  44.  
  45. #path definition
  46. diskPath = ''#'C:/Users/Pavel/Downloads/scripts/' #'D:/OpenTTD_repositories/BRIX/scripts/'
  47. inputFolder = diskPath + 'input/'
  48. outputFolder = diskPath + 'output/'
  49. tempFolder = outputFolder + 'temp/'
  50.  
  51. os.makedirs(outputFolder, exist_ok = True)
  52. os.makedirs(tempFolder, exist_ok = True)
  53.  
  54. #printing path just to check
  55. print_start(show_starting_messages, 'inputFolder is ' + inputFolder)
  56. print_start(show_starting_messages, 'outputFolder is ' + outputFolder)
  57.  
  58. #started time
  59. tt = time.time()
  60. startedTime = datetime.datetime.fromtimestamp(tt).strftime('%H:%M:%S')
  61.  
  62. #open palette image
  63. palette_img = Image.open(inputFolder + 'openttd-palette-dos-RGBA.png')
  64. #palette_img_indexed = Image.open(inputFolder + 'openttd-palette-dos-index.png')
  65. palette_img_indexed = Image.open(inputFolder + 'palette_key.png')
  66. palette_data = deepcopy(palette_img_indexed.palette)
  67. print_start(show_starting_messages, 'Opening and loading palette: ' + 'openttd-palette-dos-RGBA.png')  
  68.  
  69. p=[]
  70.  
  71. for b in range(0,palette_img.height):
  72.   for a in range(0, palette_img.width):
  73.     p.append(palette_img.getpixel((a,b)))
  74.    
  75. print_start(show_starting_messages, 'Palette loaded.')
  76.  
  77.  
  78. # ----------------------------------------------------------------------------------------------------------------
  79. # ----------------------------------------------------------------------------------------------------------------
  80. # ----------------------------------------------------------------------------------------------------------------
  81.  
  82.  
  83. def rgb2palette(args):
  84.   #print('rgb2palette args: ' + str(args))
  85.   thread_id = args[0]
  86.   input_image = args[1]
  87.   x_start = args[2]
  88.   x_end = args[3]
  89.   allowed_colour_types = args[4]
  90.   disallowed_colour_types = args[5]
  91.   allowed_colour_indexes = args[6]
  92.   disallowed_colour_indexes = args[7]
  93.   alpha_ignore = args[8]
  94.   alpha_offset_2 = args[9]
  95.   alpha_offset_1 = args[10]
  96.   red_weight = args[11]
  97.   green_weight = args[12]
  98.   blue_weight = args[13]
  99.   arg_colour_shift = args[14]
  100.   arg_debug_level = args[15]
  101.   offset_list = args[16]
  102.  
  103.   #create allowed colours list
  104.   colours_to_filter = []
  105.   for coltype in allowed_colour_types:
  106.     if coltype == 'ALL':
  107.       for n in range(1, 80):
  108.         colours_to_filter.append(n)
  109.       for n in range(88, 197):
  110.         colours_to_filter.append(n)
  111.       for n in range(205, 215):
  112.         colours_to_filter.append(n)
  113.     if coltype == 'GRAYSCALE':
  114.       for n in range(1, 16):
  115.         colours_to_filter.append(n)
  116.     if coltype == 'METAL':
  117.       for n in range(16, 24):
  118.         colours_to_filter.append(n)
  119.     if coltype == 'LIME_GREEN':
  120.       for n in range(24, 32):
  121.         colours_to_filter.append(n)
  122.     if coltype == 'BEIGE':
  123.       for n in range(32, 40):
  124.         colours_to_filter.append(n)
  125.     if coltype == 'DARK_PINK':
  126.       for n in range(40, 48):
  127.         colours_to_filter.append(n)
  128.     if coltype == 'YELLOW':
  129.       for n in range(50, 53):
  130.         colours_to_filter.append(n)
  131.     if coltype == 'DARK_BEIGE':
  132.       for n in range(53, 60):
  133.         colours_to_filter.append(n)
  134.     if coltype == 'YELLOW':
  135.       for n in range(60, 70):
  136.         colours_to_filter.append(n)
  137.     if coltype == 'BROWN_1':
  138.       for n in range(70, 80):
  139.         colours_to_filter.append(n)
  140.     if coltype == 'CC2':
  141.       for n in range(80, 88):
  142.         colours_to_filter.append(n)
  143.     if coltype == 'DARK_GREEN':
  144.       for n in range(88, 96):
  145.         colours_to_filter.append(n)
  146.     if coltype == 'PALE_GREEN':
  147.       for n in range(96, 104):
  148.         colours_to_filter.append(n)
  149.     if coltype == 'BROWN_2':
  150.       for n in range(104, 112):
  151.         colours_to_filter.append(n)
  152.     if coltype == 'BROWN_3':
  153.       for n in range(112, 122):
  154.         colours_to_filter.append(n)
  155.     if coltype == 'BROWN_4':
  156.       for n in range(122, 128):
  157.         colours_to_filter.append(n)
  158.     if coltype == 'MAUVE':
  159.       for n in range(128, 136):
  160.         colours_to_filter.append(n)
  161.     if coltype == 'PURPLE':
  162.       for n in range(136, 144):
  163.         colours_to_filter.append(n)
  164.     if coltype == 'BLUE':
  165.       for n in range(144, 154):
  166.         colours_to_filter.append(n)
  167.     if coltype == 'LIGHT_BLUE':
  168.       for n in range(154, 162):
  169.         colours_to_filter.append(n)
  170.     if coltype == 'PINK':
  171.       for n in range(162, 170):
  172.         colours_to_filter.append(n)
  173.     if coltype == 'LIGHT_PURPLE':
  174.       for n in range(170, 178):
  175.         colours_to_filter.append(n)
  176.     if coltype == 'RED_1':
  177.       for n in range(178, 185):
  178.         colours_to_filter.append(n)
  179.     if coltype == 'RED_2':
  180.       for n in range(185, 192):
  181.         colours_to_filter.append(n)
  182.     if coltype == 'ORANGE':
  183.       for n in range(192, 198):
  184.         colours_to_filter.append(n)
  185.     if coltype == 'CC1':
  186.       for n in range(198, 206):
  187.         colours_to_filter.append(n)
  188.     if coltype == 'GREEN':
  189.       for n in range(206, 210):
  190.         colours_to_filter.append(n)
  191.     if coltype =='CYAN':
  192.       for n in range(210, 215):
  193.         colours_to_filter.append(n)
  194.     if coltype == 'COLA':
  195.       for n in range(227, 232):
  196.         colours_to_filter.append(n)
  197.     if coltype == 'FIRE':
  198.       for n in range(232, 239):
  199.         colours_to_filter.append(n)
  200.     if coltype == 'LED_RED':
  201.       for n in range(239, 241):
  202.         colours_to_filter.append(n)
  203.     if coltype == 'LED_YELLOW':
  204.       for n in range(241, 245):
  205.         colours_to_filter.append(n)
  206.     if coltype == 'WATER':
  207.       for n in range(245, 255):
  208.         colours_to_filter.append(n)
  209.     if coltype == 'WHITE':
  210.       for n in range(255, 256):
  211.         colours_to_filter.append(n)
  212.  
  213.   # create disallowed colours list
  214.   disallowed_colours = []
  215.   for coltype in disallowed_colour_types:
  216.     if coltype == 'ALL':
  217.       for n in range(1, 80):
  218.         disallowed_colours.append(n)
  219.       for n in range(88, 197):
  220.         disallowed_colours.append(n)
  221.       for n in range(205, 215):
  222.         disallowed_colours.append(n)
  223.     if coltype == 'GRAYSCALE':
  224.       for n in range(1, 16):
  225.         disallowed_colours.append(n)
  226.     if coltype == 'METAL':
  227.       for n in range(16, 24):
  228.         disallowed_colours.append(n)
  229.     if coltype == 'LIME_GREEN':
  230.       for n in range(24, 32):
  231.         disallowed_colours.append(n)
  232.     if coltype == 'BEIGE':
  233.       for n in range(32, 40):
  234.         disallowed_colours.append(n)
  235.     if coltype == 'DARK_PINK':
  236.       for n in range(40, 48):
  237.         disallowed_colours.append(n)
  238.     if coltype == 'YELLOW':
  239.       for n in range(50, 53):
  240.         disallowed_colours.append(n)
  241.     if coltype == 'DARK_BEIGE':
  242.       for n in range(53, 60):
  243.         disallowed_colours.append(n)
  244.     if coltype == 'YELLOW':
  245.       for n in range(60, 70):
  246.         disallowed_colours.append(n)
  247.     if coltype == 'BROWN_1':
  248.       for n in range(70, 80):
  249.         disallowed_colours.append(n)
  250.     if coltype == 'CC2':
  251.       for n in range(80, 88):
  252.         disallowed_colours.append(n)
  253.     if coltype == 'DARK_GREEN':
  254.       for n in range(88, 96):
  255.         disallowed_colours.append(n)
  256.     if coltype == 'PALE_GREEN':
  257.       for n in range(96, 104):
  258.         disallowed_colours.append(n)
  259.     if coltype == 'BROWN_2':
  260.       for n in range(104, 112):
  261.         disallowed_colours.append(n)
  262.     if coltype == 'BROWN_3':
  263.       for n in range(112, 122):
  264.         disallowed_colours.append(n)
  265.     if coltype == 'BROWN_4':
  266.       for n in range(122, 128):
  267.         disallowed_colours.append(n)
  268.     if coltype == 'MAUVE':
  269.       for n in range(128, 136):
  270.         disallowed_colours.append(n)
  271.     if coltype == 'PURPLE':
  272.       for n in range(136, 144):
  273.         disallowed_colours.append(n)
  274.     if coltype == 'BLUE':
  275.       for n in range(144, 154):
  276.         disallowed_colours.append(n)
  277.     if coltype == 'LIGHT_BLUE':
  278.       for n in range(154, 162):
  279.         disallowed_colours.append(n)
  280.     if coltype == 'PINK':
  281.       for n in range(162, 170):
  282.         disallowed_colours.append(n)
  283.     if coltype == 'LIGHT_PURPLE':
  284.       for n in range(170, 178):
  285.         disallowed_colours.append(n)
  286.     if coltype == 'RED_1':
  287.       for n in range(178, 185):
  288.         disallowed_colours.append(n)
  289.     if coltype == 'RED_2':
  290.       for n in range(185, 192):
  291.         disallowed_colours.append(n)
  292.     if coltype == 'ORANGE':
  293.       for n in range(192, 198):
  294.         disallowed_colours.append(n)
  295.     if coltype == 'CC1':
  296.       for n in range(198, 206):
  297.         disallowed_colours.append(n)
  298.     if coltype == 'GREEN':
  299.       for n in range(206, 210):
  300.         disallowed_colours.append(n)
  301.     if coltype =='CYAN':
  302.       for n in range(210, 215):
  303.         disallowed_colours.append(n)
  304.     if coltype == 'COLA':
  305.       for n in range(227, 232):
  306.         disallowed_colours.append(n)
  307.     if coltype == 'FIRE':
  308.       for n in range(232, 239):
  309.         disallowed_colours.append(n)
  310.     if coltype == 'LED_RED':
  311.       for n in range(239, 241):
  312.         disallowed_colours.append(n)
  313.     if coltype == 'LED_YELLOW':
  314.       for n in range(241, 245):
  315.         disallowed_colours.append(n)
  316.     if coltype == 'WATER':
  317.       for n in range(245, 255):
  318.         disallowed_colours.append(n)
  319.     if coltype == 'WHITE':
  320.       for n in range(255, 256):
  321.         disallowed_colours.append(n)
  322.  
  323.   #add individual disallowed colours
  324.   for n in disallowed_colour_indexes:
  325.     disallowed_colours.append(n)
  326.  
  327.   #move non-disallowed colours to a new list
  328.   filtered_colours_to_filter = []
  329.   for c in colours_to_filter:
  330.     skip = 0
  331.     for d in disallowed_colours:
  332.       if c == d:
  333.         skip = 1
  334.     if skip == 0:
  335.       filtered_colours_to_filter.append(c)
  336.  
  337.   #add individual allowed colours
  338.   for n in allowed_colour_indexes:
  339.     filtered_colours_to_filter.append(n)
  340.  
  341.   print_lvl_4(arg_debug_level, 'Colours to filter: ' + str(filtered_colours_to_filter))
  342.  
  343.   #open input image
  344.   i = Image.open(inputFolder + input_image + '.png')
  345.   #i.show()
  346.   print_lvl_2(arg_debug_level, 'Thread ' + str(thread_id) + ' Opening: ' + input_image + '.png')  
  347.  
  348.   #create new empty image for output
  349.   imageOutput = Image.new('L', (i.width,i.height), color = 0)
  350.  
  351.  
  352.   for y in range (0, i.height):
  353.     #timeStamp
  354.     ts = time.time()
  355.     timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S')
  356.     if y%32 == 0:
  357.       print_lvl_5(arg_debug_level, 'Thread ' + str(thread_id) + ' ' + timeStamp + ' - ' + input_image + ' row {}'.format(y))
  358.     for x in range (x_start, x_end):
  359.       #defining winner variables
  360.       winnerDistance = 100000000
  361.       winnerID = 0
  362.      
  363.       #loading pixel from image and separating RGBA
  364.       pixelNumber = x + (y * i.width)
  365.       pix = i.getpixel((x,y))
  366.       pixRed = pix[0]
  367.       pixGreen = pix[1]
  368.       pixBlue = pix[2]
  369.       pixAlpha = pix[3]
  370.  
  371.       #check Alpha in pixel, and output alpha/color offset
  372.       if pixAlpha < alpha_ignore:
  373.         finalAlpha = 0
  374.         colorOffset = 0
  375.       if pixAlpha >= alpha_ignore and pixAlpha < 178:
  376.         finalAlpha = 255
  377.         colorOffset = 2
  378.       if pixAlpha >= alpha_offset_1 and pixAlpha < alpha_offset_2:
  379.         finalAlpha = 255
  380.         colorOffset = 1
  381.       if pixAlpha >= alpha_offset_2:
  382.         finalAlpha = 255
  383.         colorOffset = 0
  384.          
  385.       #if alpha above 50%, do colour comparing to palette
  386.       if pixAlpha >= alpha_ignore:
  387.         for colour_id in filtered_colours_to_filter:
  388.           rgb1 = p[colour_id]
  389.           r1 = rgb1[0]
  390.           g1 = rgb1[1]
  391.           b1 = rgb1[2]
  392.  
  393.           l1 = (r1*red_weight + g1*green_weight + b1*blue_weight) / 255000
  394.           l2 = (pixRed*red_weight + pixGreen*green_weight + pixBlue*blue_weight) / 255000
  395.           dL = l1-l2
  396.           dR = (r1-pixRed)/255
  397.           dG = (g1-pixGreen)/255
  398.           dB = (b1-pixBlue)/255
  399.           distance = (dR*dR*red_weight*0.001 + dG*dG*blue_weight*0.001 + dB*dB*blue_weight*0.001)*0.75 + dL*dL
  400.          
  401.           if distance < winnerDistance:
  402.             winnerDistance = distance
  403.             winnerID = colour_id
  404.  
  405.       #final color changed by colorOffset
  406.       finalID = offset_list[winnerID][colorOffset] #winnerID - colorOffset
  407.       #finalAlpha taken from the if output above palette colour comparing
  408.  
  409.       if colorOffset == 2:
  410.         for g in disallowed_colours:
  411.           if g == finalID:
  412.             colorOffset = 1
  413.             finalID = offset_list[winnerID][colorOffset]
  414.       if colorOffset == 1:
  415.         for g in disallowed_colours:
  416.           if g == finalID:
  417.             colorOffset = 0
  418.             finalID = offset_list[winnerID][colorOffset]
  419.          
  420.       #argument to ignore colorOffset
  421.       if arg_colour_shift == False:
  422.         finalID = winnerID
  423.  
  424.       #put the final pixel into the output picture
  425.       imageOutput.putpixel((x,y),(finalID))
  426.       #print('Thread ' + str(thread_id) + ' Pixel ' + str(pixelNumber) + ': R= ' + str(finalR) + ', G= ' + str(finalG) + ', B= ' + str(finalB) + ', A= '  + str(finalAlpha)   )
  427.  
  428.   cropped_imageOutput = imageOutput.crop((x_start, 0,x_end,i.height))
  429.   cropped_imageOutput.putpalette(palette_data)
  430.   cropped_imageOutput.save(outputFolder + 'temp/' + 'temp' + '_'  + str(thread_id) + '_8bpp.png') #assumes _#### frame number format (removes last 5 characters)
  431.   i.close()
  432.   cropped_imageOutput.close()    
  433.    
  434. def combineResults(args, thread_count, palette_data, arg_debug_level):
  435.   print_lvl_5(arg_debug_level, 'combineResults args: ' + str(args))
  436.   thread_id = args[0]
  437.   combine_input_image = args[1]
  438.   x_start = args[2]
  439.   x_end = args[3]
  440.  
  441.   image_for_resolution = Image.open(inputFolder + str(combine_input_image[1]) + '.png')
  442.   final_image = Image.new('L', (image_for_resolution.width, image_for_resolution.height), color = 0)
  443.   final_image.putpalette(palette_data)
  444.  
  445.   print_lvl_3(arg_debug_level, 'img_resolution.height = ' + str(image_for_resolution.height))
  446.   for combine_order in args:
  447.     image_to_paste = Image.open(outputFolder + 'temp/' + 'temp' + '_'  + str(combine_order[0]) + '_8bpp.png')
  448.    
  449.     print_lvl_3(arg_debug_level, 'Combine strip with x_start = ' + str(combine_order[2]))
  450.  
  451.     final_image.paste(image_to_paste, box = (combine_order[2], 0))
  452.  
  453.   final_image.putpalette(palette_data)
  454.   final_image.save(outputFolder + str(combine_input_image[1]) + '_8bpp.png')
  455.  
  456. def check_list_count(arg_debug_level, list_to_check, n):
  457.   w = 0
  458.   for stuff in list_to_check:
  459.     w += 1
  460.   print_lvl_5(arg_debug_level, 'Offset list item count: ' + str(w) + ' , last n is: ' + str(n))
  461.  
  462.  
  463. def append_offset_list(offset_list, temp_list):
  464.   for t in temp_list:
  465.     offset_list.append(t)
  466.  
  467. def add_6_index_list(offset_list, n):
  468.     temp_list = [
  469.         [   n,  n+1,  n+2],#1
  470.         [ n+1,  n+2,  n+3],#2
  471.         [ n+2,  n+3,  n+3],#3--
  472.         [ n+3,  n+3,  n+2],#4--
  473.         [ n+4,  n+3,  n+2],#5
  474.         [ n+5,  n+4,  n+3]# 6
  475.       ]
  476.     append_offset_list(offset_list,temp_list)
  477.  
  478. def add_8_index_list(offset_list, n):
  479.     temp_list = [
  480.         [   n,  n+1,  n+2],#1
  481.         [ n+1,  n+2,  n+3],#2
  482.         [ n+2,  n+3,  n+4],#3
  483.         [ n+3,  n+4,  n+4],#4--
  484.         [ n+4,  n+4,  n+3],#5--
  485.         [ n+5,  n+4,  n+3],#6
  486.         [ n+6,  n+5,  n+4],#7
  487.         [ n+7,  n+6,  n+5]# 8
  488.       ]
  489.     append_offset_list(offset_list,temp_list)
  490.      
  491. def add_10_index_list(offset_list, n):
  492.     temp_list = [
  493.         [   n,  n+1,  n+2],#1
  494.         [ n+1,  n+2,  n+3],#2
  495.         [ n+2,  n+3,  n+4],#3
  496.         [ n+3,  n+4,  n+5],#4
  497.         [ n+4,  n+5,  n+5],#5--
  498.         [ n+5,  n+5,  n+4],#6--
  499.         [ n+6,  n+5,  n+4],#7
  500.         [ n+7,  n+6,  n+5],#8
  501.         [ n+8,  n+7,  n+6],#9
  502.         [ n+9,  n+8,  n+7]#10
  503.       ]
  504.     append_offset_list(offset_list,temp_list)
  505.      
  506.      
  507. def run():
  508.   # ----------------------------------------------------------------------------------------------------------------
  509.   #VARIABLES
  510.   # ----------------------------------------------------------------------------------------------------------------
  511.   job_list = [  
  512.       [
  513.       options['input_name'],        
  514.       options['allowed_colour_types'],#allowed colour types (string list)
  515.       options['disallowed_colour_types'],#disallowed colour types (string list)
  516.       options['allowed_colour_indexes'],#allowed colour indexes (number list)
  517.       options['disallowed_colour_indexes'],#disallowed colour indexes (number list)
  518.       options['alpha_ignore'],
  519.       options['alpha_offset_2'],
  520.       options['alpha_offset_1'],
  521.       options['red_weight'],#red weight (number, default = 1)
  522.       options['green_weight'],#green weight (number, default = 1)
  523.       options['blue_weight'],#blue weight (number, default = 1)
  524.       options['colour_shift'],
  525.       options['debug_level']
  526.       ]
  527.     ]
  528.  
  529.   # ----------------------------------------------------------------------------------------------------------------
  530.   thread_count = options['thread_count']
  531.   # ----------------------------------------------------------------------------------------------------------------
  532.   # ----------------------------------------------------------------------------------------------------------------
  533.   # ----------------------------------------------------------------------------------------------------------------
  534.  
  535.  
  536.   #--------------------------------------------------------------------------------
  537.   #creating offset list -----------------------------------------------------------
  538.   #--------------------------------------------------------------------------------
  539.   offset_list = []
  540.  
  541.   #TRANSPARENCY
  542.   n = 0
  543.   check_list_count(options['debug_level'], offset_list, n)
  544.   temp_list = [
  545.     [n,n,n]
  546.   ]
  547.   append_offset_list(offset_list,temp_list)
  548.  
  549.  
  550.   #GRAYSCALE
  551.   n = 1
  552.   check_list_count(options['debug_level'], offset_list, n)
  553.   temp_list = [
  554.       [   n,  n+1,  n+2],#1
  555.       [ n+1,  n+2,  n+3],#2
  556.       [ n+2,  n+3,  n+4],#3
  557.       [ n+3,  n+4,  n+5],#4
  558.       [ n+4,  n+5,  n+6],#5
  559.       [ n+5,  n+6,  n+7],#6
  560.       [ n+6,  n+7,  n+7],#7--
  561.       [ n+7,  n+8,  n+8],#8--
  562.       [ n+8,  n+8,  n+7],#9--
  563.       [ n+9,  n+8,  n+7],#10
  564.       [n+10,  n+9,  n+8],#11
  565.       [n+11, n+10,  n+9],#12
  566.       [n+12, n+11, n+10],#13
  567.       [n+13, n+12, n+11],#14
  568.       [n+14, n+13, n+12],#15
  569.   ]
  570.   append_offset_list(offset_list, temp_list)
  571.  
  572.  
  573.   #METAL
  574.   n = 16
  575.   check_list_count(options['debug_level'], offset_list, n)
  576.   add_8_index_list(offset_list, n)
  577.  
  578.  
  579.   #LIME_GREEN
  580.   n = 24
  581.   check_list_count(options['debug_level'], offset_list, n)
  582.   add_8_index_list(offset_list, n)
  583.  
  584.  
  585.   #BEIGE
  586.   n = 32
  587.   check_list_count(options['debug_level'], offset_list, n)
  588.   add_8_index_list(offset_list, n)
  589.  
  590.  
  591.   #DARK_PINK
  592.   n = 40
  593.   check_list_count(options['debug_level'], offset_list, n)
  594.   add_10_index_list(offset_list, n)
  595.  
  596.  
  597.   #YELLOW
  598.   n = 50
  599.   check_list_count(options['debug_level'], offset_list, n)
  600.   temp_list = [
  601.     [   n,  189,  188],#1
  602.     [ n+1,  n+0,  188],#2
  603.     [ n+2,  n+1,  n+0]# 3
  604.   ]
  605.   append_offset_list(offset_list, temp_list)
  606.  
  607.  
  608.   #DARK_BEIGE
  609.   n = 53
  610.   check_list_count(options['debug_level'], offset_list, n)
  611.   temp_list = [
  612.     [   n,  n+1,  n+2],#1
  613.     [ n+1,  n+2,  n+3],#2
  614.     [ n+2,  n+3,  n+3],#3--
  615.     [ n+3,  n+3,  n+4],#4--
  616.     [ n+4,  n+3,  n+3],#5--
  617.     [ n+5,  n+4,  n+3],#6
  618.     [ n+6,  n+5,  n+4]# 7
  619.   ]
  620.   append_offset_list(offset_list, temp_list)
  621.  
  622.  
  623.   #YELLOW
  624.   n = 60
  625.   check_list_count(options['debug_level'], offset_list, n)
  626.   add_10_index_list(offset_list, n)
  627.  
  628.  
  629.   #BROWN_1
  630.   n = 70
  631.   check_list_count(options['debug_level'], offset_list, n)
  632.   add_10_index_list(offset_list, n)
  633.  
  634.  
  635.   #CC2,
  636.   n = 80
  637.   check_list_count(options['debug_level'], offset_list, n)
  638.   add_8_index_list(offset_list, n)
  639.  
  640.  
  641.   #DARK_GREEN
  642.   n = 88
  643.   check_list_count(options['debug_level'], offset_list, n)
  644.   add_8_index_list(offset_list, n)
  645.  
  646.  
  647.   #PALE_GREEN
  648.   n = 96
  649.   check_list_count(options['debug_level'], offset_list, n)
  650.   add_8_index_list(offset_list, n)
  651.  
  652.  
  653.   #BROWN_2
  654.   n = 104
  655.   check_list_count(options['debug_level'], offset_list, n)
  656.   add_8_index_list(offset_list, n)
  657.  
  658.  
  659.   #BROWN_3
  660.   n = 112
  661.   check_list_count(options['debug_level'], offset_list, n)
  662.   add_10_index_list(offset_list, n)
  663.  
  664.  
  665.   #BROWN_4
  666.   n = 122
  667.   check_list_count(options['debug_level'], offset_list, n)
  668.   add_6_index_list(offset_list, n)
  669.  
  670.  
  671.   #MAUVE
  672.   n = 128
  673.   check_list_count(options['debug_level'], offset_list, n)
  674.   add_8_index_list(offset_list, n)
  675.  
  676.  
  677.   #PURPLE
  678.   n = 136
  679.   check_list_count(options['debug_level'], offset_list, n)
  680.   add_8_index_list(offset_list, n)
  681.  
  682.  
  683.   #BLUE
  684.   n = 144
  685.   check_list_count(options['debug_level'], offset_list, n)
  686.   add_10_index_list(offset_list, n)
  687.  
  688.  
  689.   #LIGHT_BLUE
  690.   n = 154
  691.   check_list_count(options['debug_level'], offset_list, n)
  692.   add_8_index_list(offset_list, n)
  693.  
  694.  
  695.   #PINK
  696.   n = 162
  697.   check_list_count(options['debug_level'], offset_list, n)
  698.   add_8_index_list(offset_list, n)
  699.  
  700.  
  701.   #LIGHT_PURPLE
  702.   n = 170
  703.   check_list_count(options['debug_level'], offset_list, n)
  704.   add_8_index_list(offset_list, n)
  705.  
  706.  
  707.   #RED
  708.   n = 178
  709.   check_list_count(options['debug_level'], offset_list, n)
  710.   temp_list = [
  711.     [   n,  n+1,  n+2],#1
  712.     [ n+1,  n+2,  n+3],#2
  713.     [ n+2,  n+3,  n+4],#3
  714.     [ n+3,  n+4,  n+5],#4
  715.     [ n+4,  n+5,  n+6],#5
  716.     [ n+5,  n+6,  n+7],#6
  717.     [ n+6,  n+7,  n+7],#7--
  718.     [ n+7,  n+6,  n+6],#8--
  719.     [ n+8,  n+7,  n+6],#9
  720.     [ n+9,  n+8,  n+7],#10
  721.     [n+10,  n+9,  n+8],#11
  722.     [n+11, n+10,  n+9],#12
  723.     [n+12, n+11, n+10],#13
  724.     [n+13, n+12, n+11]#14
  725.   ]
  726.   append_offset_list(offset_list, temp_list)
  727.  
  728.  
  729.   #ORANGE
  730.   n = 192
  731.   check_list_count(options['debug_level'], offset_list, n)
  732.   temp_list = [
  733.     [  n,   64,  63],#1
  734.     [ n+1, n+0,  64],#2
  735.     [ n+2, n+1, n+0],#3
  736.     [ n+3, n+2,  n+1],#4
  737.     [ n+4, n+3, n+2],#5
  738.     [ n+5, n+4, n+3]#6
  739.   ]
  740.   append_offset_list(offset_list, temp_list)
  741.  
  742.  
  743.   #CC1
  744.   n = 198
  745.   check_list_count(options['debug_level'], offset_list, n)
  746.   add_8_index_list(offset_list, n)
  747.  
  748.  
  749.   #GREEN
  750.   n = 206
  751.   check_list_count(options['debug_level'], offset_list, n)
  752.   temp_list = [
  753.     [    n, 93,  92],#1
  754.     [ n+1, n+0,  93],#2
  755.     [ n+2, n+1, n+0],#3
  756.     [ n+3, n+2, n+1]# 4
  757.   ]
  758.   append_offset_list(offset_list, temp_list)
  759.  
  760.  
  761.   #CYAN
  762.   n = 210
  763.   check_list_count(options['debug_level'], offset_list, n)
  764.   temp_list = [
  765.     [  n, n+1, n+2],#1
  766.     [n+1, n+2, n+2],#2
  767.     [n+2, n+3, n+3],#3
  768.     [n+3, n+2, n+2],#4
  769.     [n+4, n+3, n+2]# 5
  770.   ]
  771.   append_offset_list(offset_list, temp_list)
  772.  
  773.  
  774.   #ALPHAPINK & ACT
  775.   n = 215
  776.   check_list_count(options['debug_level'], offset_list, n)
  777.   temp_list = [
  778.     [  n+0,  n+0,  n+0],#1  - index 215
  779.     [  n+1,  n+1,  n+1],#2  - index 216
  780.     [  n+2,  n+2,  n+2],#3  - index 217
  781.     [  n+3,  n+3,  n+3],#4  - index 218
  782.     [  n+4,  n+4,  n+4],#5  - index 219
  783.     [  n+5,  n+5,  n+5],#6  - index 220
  784.     [  n+6,  n+6,  n+6],#7  - index 221
  785.     [  n+7,  n+7,  n+7],#8  - index 222
  786.     [  n+8,  n+8,  n+8],#9  - index 223
  787.     [  n+9,  n+9,  n+9],#10 - index 224
  788.     [ n+10, n+10, n+10],#11 - index 225
  789.     [ n+11, n+11, n+11],#12 - index 226
  790.     [ n+12, n+12, n+12],#13 - index 227
  791.     [ n+13, n+13, n+13],#14 - index 228
  792.     [ n+14, n+14, n+14],#15 - index 229
  793.     [ n+15, n+15, n+15],#16 - index 230
  794.     [ n+16, n+16, n+16],#17 - index 231
  795.     [ n+17, n+17, n+17],#18 - index 232
  796.     [ n+18, n+18, n+18],#19 - index 233
  797.     [ n+19, n+19, n+19],#20 - index 234
  798.     [ n+20, n+20, n+20],#21 - index 235
  799.     [ n+21, n+21, n+21],#22 - index 236
  800.     [ n+22, n+22, n+22],#23 - index 237
  801.     [ n+23, n+23, n+23],#24 - index 238
  802.     [ n+24, n+24, n+24],#25 - index 239
  803.     [ n+25, n+25, n+25],#26 - index 240
  804.     [ n+26, n+26, n+26],#27 - index 241
  805.     [ n+27, n+27, n+27],#28 - index 242
  806.     [ n+28, n+28, n+28],#29 - index 243
  807.     [ n+29, n+29, n+29],#30 - index 244
  808.     [ n+30, n+30, n+30],#31 - index 245
  809.     [ n+31, n+31, n+31],#32 - index 246
  810.     [ n+32, n+32, n+32],#33 - index 247
  811.     [ n+33, n+33, n+33],#34 - index 248
  812.     [ n+34, n+34, n+34],#35 - index 249
  813.     [ n+35, n+35, n+35],#36 - index 250
  814.     [ n+36, n+36, n+36],#37 - index 251
  815.     [ n+37, n+37, n+37],#38 - index 252
  816.     [ n+38, n+38, n+38],#39 - index 253
  817.     [ n+39, n+39, n+39],#40 - index 254
  818.     [ n+40, n+40, n+40] #41 - index 255
  819.   ]
  820.   append_offset_list(offset_list, temp_list)
  821.  
  822.   n = 256
  823.   check_list_count(options['debug_level'], offset_list, n)
  824.  
  825.   for job in job_list:
  826.     all_jobs = []
  827.     job_chunks = []
  828.     queue = []
  829.  
  830.     extra = 0
  831.     chunk_image = Image.open(inputFolder + job[0] + '.png')
  832.     chunk_average_size = math.floor(chunk_image.width/thread_count)
  833.     chunk_modulo_size = chunk_image.width % thread_count
  834.     for thread in range(0, thread_count):
  835.       start = chunk_average_size * thread
  836.       end = (chunk_average_size * (thread+1))
  837.  
  838.       start += extra
  839.       if thread < chunk_modulo_size:  
  840.         extra += 1
  841.       end += extra
  842.  
  843.       start_and_end = [
  844.                         thread, #0 - threadID
  845.                         job[0], #1 - input_name
  846.                         start,  #2 - x_start
  847.                         end,    #3 - x_end
  848.                         job[1], #4 - allowed_colour_types
  849.                         job[2], #5 - disallowed_colour_types
  850.                         job[3], #6 - allowed_colour_indexes
  851.                         job[4], #7 - disallowed_colour_indexes
  852.                         job[5], #8 - alpha_ignore
  853.                         job[6], #9 - alpha_offset_1
  854.                         job[7], #10- alpha_offset_2
  855.                         job[8], #11- red_weight
  856.                         job[9], #12- green_weight
  857.                         job[10],#13- blue_weight
  858.                         job[11],#14- colour_shift
  859.                         job[12],#15- debug_level
  860.                         offset_list
  861.                       ]
  862.       job_chunks.append(start_and_end)
  863.       queue.append( [thread, job[0], start, end, job[1], job[2], job[3], job[4], job[5], job[6], job[7], job[8], job[9], job[10], job[11], job[12], offset_list ])
  864.  
  865.     all_jobs.append(job_chunks)
  866.  
  867.     for a_job in all_jobs:
  868.       print_lvl_5(options['debug_level'], '-'*32)
  869.       thread_id = 0
  870.       for b_thread in a_job:
  871.         if thread_id == 0:
  872.           thread_id += 1
  873.           print_lvl_5(options['debug_level'], 'Job: ' + str(b_thread[0]))
  874.           print_lvl_5(options['debug_level'], ' '*10 + 'Start, ' + 'End')
  875.           print_lvl_5(options['debug_level'], 'Thread ' + str(thread_id) + ': ' + str(b_thread[1]) + ', ' + str(b_thread[2]))#
  876.        
  877.        
  878.     print_lvl_7(options['debug_level'], (queue))
  879.    
  880.  
  881.     # ----------------------------------------------------------------------------------------------------------------
  882.     # PROCESS
  883.     # ----------------------------------------------------------------------------------------------------------------
  884.    
  885.     pool = multiprocessing.Pool(processes = thread_count)
  886.     pool.map(rgb2palette, queue)
  887.     pool.close()
  888.     pool.join()
  889.    
  890.     combineResults(queue, thread_count, palette_data, options['debug_level'])
  891.  
  892.     #finished time
  893.     tx = time.time()
  894.     finishedTime = datetime.datetime.fromtimestamp(tx).strftime('%H:%M:%S')
  895.    
  896.     print('Started:  ' + startedTime)
  897.     print('Finished: ' + finishedTime)
  898.    
  899.  
  900. #run()
  901.  
  902. if __name__ == '__main__':
  903.   parser = argparse.ArgumentParser(description = 'Process some arguments.')
  904.   parser.add_argument('-t','--thread_count',
  905.                       help='Number of theads to run, Default: 16',
  906.                       type = int,
  907.                       required = False)
  908.   parser.add_argument('-n','--input_name',
  909.                       help='File to process. Without .png extension. File can only be RGBA (not RGB)',
  910.                       required = True)
  911.   parser.add_argument('-c','--allowed_colour_types',
  912.                       help='Allowed colour types (list of strings), Default: "ALL"',
  913.                       nargs = '+',
  914.                       required = False)
  915.   parser.add_argument('-d','--disallowed_colour_types',
  916.                       help='Disallowed colour types (list of strings), Default: nothing',
  917.                       nargs = '+',
  918.                       required = False)
  919.   parser.add_argument('-e','--allowed_colour_indexes',
  920.                       help='Allowed colour indexes (list of numbers), Default: nothing',
  921.                       nargs = '+',
  922.                       type = int,
  923.                       required = False)
  924.   parser.add_argument('-f','--disallowed_colour_indexes',
  925.                       help='Disallowed colour indexes (list of numbers), Default: nothing',
  926.                       nargs = '+',
  927.                       type = int,
  928.                       required = False)
  929.   parser.add_argument('-i','--alpha_ignore',
  930.                       help='Threshold of ignoring transparency. Default: 128',
  931.                       type = int,
  932.                       required = False)
  933.   parser.add_argument('-k','--alpha_offset_1',
  934.                       help='Threshold of transparency to colour shift by 1 index. Default: 178',
  935.                       type = int,
  936.                       required = False)
  937.   parser.add_argument('-j','--alpha_offset_2',
  938.                       help='Threshold of transparency to colour shift by 2 indexes. Default: 230',
  939.                       type = int,
  940.                       required = False)
  941.   parser.add_argument('-o','--red_weight',
  942.                       help='Weight of red input for colour comparing. Default: 1',
  943.                       type = float,
  944.                       required = False)
  945.   parser.add_argument('-p','--green_weight',
  946.                       help='Weight of green input for colour comparing. Default: 1',
  947.                       type = float,
  948.                       required = False)
  949.   parser.add_argument('-q','--blue_weight',
  950.                       help='Weight of blue input for colour comparing. Default: 1',
  951.                       type = float,
  952.                       required = False)
  953.   parser.add_argument('-r','--colour_shift',
  954.                       help='For semi-transparent pixels, shifts index to attempt to compensate alpha. Default: True',
  955.                       type = bool,
  956.                       required = False)
  957.   parser.add_argument('-s', '--debug_level',
  958.                       help='Amount of info shown in console. Default: 1, Min: 1, Max: 7',
  959.                       type = int,
  960.                       required = False)
  961.   options = vars(parser.parse_args())
  962.  
  963.   if not options['thread_count']:
  964.     options['thread_count'] = 16
  965.   if not options['allowed_colour_types']:
  966.     options['allowed_colour_types'] = ['ALL']
  967.   if not options['disallowed_colour_types']:
  968.     options['disallowed_colour_types'] = []
  969.   if not options['allowed_colour_indexes']:
  970.     options['allowed_colour_indexes'] = []
  971.   if not options['disallowed_colour_indexes']:
  972.     options['disallowed_colour_indexes'] = []
  973.   if not options['alpha_ignore']:
  974.     options['alpha_ignore'] = 128
  975.   if not options['alpha_offset_1']:
  976.     options['alpha_offset_1'] = 178
  977.   if not options['alpha_offset_2']:
  978.     options['alpha_offset_2'] = 230
  979.   if not options['red_weight']:
  980.     options['red_weight'] = 1
  981.   if not options['green_weight']:
  982.     options['green_weight'] = 1
  983.   if not options['blue_weight']:
  984.     options['blue_weight'] = 1
  985.   if not options['colour_shift']:
  986.     options['colour_shift'] = False
  987.   if not options['debug_level']:
  988.     options['debug_level'] = 1
  989.  
  990.   run()

Comments