AgentM

texture_colors

Oct 16th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 21.31 KB | None | 0 0
  1. #! python3
  2. #### Use python 3.7, required libs: PIL (pip install Pillow) and Numpy (pip install numpy)
  3. #### By AgentM
  4. #### https://pastebin.com/c9CNndT6
  5. #### Last update: Oct 21, 2019 @ 21:50 UTC+1:00
  6. #### Just run the file using python <this_file_name>
  7. #### Alternatively you can run it with ./<this_file_name>
  8.  
  9. import numpy as np
  10. import shutil
  11. import os
  12. import sys
  13. import time
  14. import json
  15.  
  16. from PIL import Image, ImageOps, ImageStat
  17. from os import walk
  18. from os import mkdir
  19. from os import listdir
  20. from os.path import exists
  21. from os.path import isfile
  22. from os.path import isdir
  23. from os.path import join as path_join
  24. from shutil import copyfile
  25. from threading import Thread
  26.  
  27. IN = 'in'
  28. OUT = 'out'
  29.  
  30. ################################## MAIN ##################################
  31.  
  32. def main():
  33.     args = sys.argv[1:]
  34.     if (len(args) > 0):
  35.         if (args[0] == 'help'):
  36.             show_help()
  37.         elif (args[0] == 'auto-contrast'):
  38.             benchmark(auto_contrast_routine)
  39.         elif (args[0] == 'colorize'):
  40.             if (len(args) > 6):
  41.                 colors = ((float(args[1]), float(args[2]), float(args[3])), (float(args[4]), float(args[5]), float(args[6])))
  42.                 benchmark(colorize_routine, colors)
  43.             else:
  44.                 print('Missing argument <colors>')
  45.         elif (args[0] == 'color'):
  46.             if (len(args) > 3):
  47.                 color = (float(args[1]), float(args[2]), float(args[3]))
  48.                 benchmark(color_routine, color)
  49.             else:
  50.                 print('Missing argument <color>')
  51.         elif (args[0] == 'grayscale'):
  52.             benchmark(grayscale_routine)
  53.         elif (args[0] == 'blend'):
  54.             if (len(args) > 1):
  55.                 alpha = 0.5
  56.                 if (len(args) > 2):
  57.                     alpha = float(args[2])
  58.                 benchmark(blend_routine, (args[1], alpha))
  59.             else:
  60.                 print('Missing argument <blend img>')
  61.         elif (args[0] == 'shift-hue'):
  62.             amount = 1
  63.             if (len(args) > 1):
  64.                 amount = int(args[1])
  65.             benchmark(hue_shift_routine, amount)
  66.         elif(args[0] == 'posterize'):
  67.             bits = 3
  68.             if (len(args) > 1):
  69.                 bits = int(args[1])
  70.             benchmark(posterize_routine, bits)
  71.         elif(args[0] == 'solarize'):
  72.             threshold = 0.5
  73.             if (len(args) > 1):
  74.                 threshold = float(args[1])
  75.             benchmark(solarize_routine, threshold)
  76.         elif(args[0] == 'mean'):
  77.             benchmark(mean_routine)
  78.         elif (args[0] == 'invert'):
  79.             if (len(args) > 1):
  80.                 benchmark(invert_routine, to_bool(args[1]))
  81.             else:
  82.                 benchmark(invert_routine)
  83.         elif (args[0] == 'threshold'):
  84.             if (len(args) > 1):
  85.                 benchmark(threshold_routine, float(args[1]))
  86.             else:
  87.                 benchmark(threshold_routine)
  88.         elif (args[0] == 'rainbow'):
  89.             if (len(args) > 1):
  90.                 interpolate = True
  91.                 speed = int(args[1])
  92.                 if (len(args) > 2):
  93.                     interpolate = args[2].lower() == 'true'
  94.                 benchmark(rainbow_routine, (speed, interpolate))
  95.             else:
  96.                 benchmark(rainbow_routine)
  97.         else:
  98.             usage()
  99.     else:
  100.         usage()
  101.  
  102. ############################## COMMAND INFO ##############################
  103.  
  104. def usage():
  105.     file_name = sys.argv[0]
  106.     print(' Usage: python %s <mode> [mode arguments]' % file_name)
  107.  
  108. def show_help():
  109.     file_name = sys.argv[0]
  110.     print(' Usage: %s <mode> [mode arguments]' % file_name)
  111.     print('    or: %s help                         View this help page.' % file_name)
  112.     print(' ')
  113.     print(' Available modes:')
  114.     print('    %s auto-contrast                    Amplify the contrast to the max!' % file_name)
  115.     print('    %s blend <blend img> [alpha]        Interpolate with blend img using alpha!' % file_name)
  116.     print('    %s color <r> <g> <b>                Convert to a color as r, g and b (between 0 and 1 inclusive)' % file_name)
  117.     print('    %s colorize <black_rgb> <white_rgb> Colorize for white and black colors' % file_name)
  118.     print('    %s grayscale                        Convert to grayscale' % file_name)
  119.     print('    %s invert [invert-alpha]            Invert all colors (and alpha, if passed "True")' % file_name)
  120.     print('    %s mean                             Takes the mean color value and uses it to fill the image' % file_name)
  121.     print('    %s posterize [bits]                 Change the bit-depth of the image' % file_name)
  122.     print('    %s shift-hue [amount]               Shifts hue r->g->b->r by "amount" times' % file_name)
  123.     print('    %s solarize [threshold]             Inverts any pixels above the threshold' % file_name)
  124.     print('    %s rainbow [time] [interpolate]     ???' % file_name)
  125.     print('    %s threshold [cut-off]              All values on or above the cut-off will be 255, all others: 0' % file_name)
  126.  
  127.  
  128. ################################ ROUTINES ################################
  129.  
  130. def rainbow_routine(args=(20, True)):
  131.     csi = rainbow_color_image()
  132.     alpha = 0.25
  133.     print('Applying "Rainbow (%s, %s)":' % (str(args[0]), str(args[1])))
  134.     convert_all_file_locations_special(rainbow, blend_no_alpha, args, (csi, alpha))
  135.     print('Done!')
  136.  
  137. def solarize_routine(threshold = 0.5):
  138.     print('Applying "Solarize (%s)":' % str(threshold))
  139.     convert_all_file_locations(solarize, threshold)
  140.     print('Done!')
  141.  
  142. def posterize_routine(bits = 3):
  143.     print('Applying "Posterize (%s)":' % str(bits))
  144.     convert_all_file_locations(posterize, bits)
  145.     print('Done!')
  146.  
  147. def auto_contrast_routine():
  148.     print('Applying "Auto Contrast":')
  149.     convert_all_file_locations(auto_contrast)
  150.     print('Done!')
  151.  
  152. def blend_routine(args):
  153.     csi = Image.open(args[0]).convert('RGBA')
  154.     print('Applying "Colorize (%s, %s)":' % (args[0], str(args[1])))
  155.     convert_all_file_locations(blend, (csi, args[1]))
  156.     print('Done!')
  157.  
  158. def colorize_routine(colors):
  159.     print('Applying "Colorize (%s, %s)":' % (str(colors[0]), str(colors[1])))
  160.     convert_all_file_locations(colorize, colors)
  161.     print('Done!')
  162.  
  163. def mean_routine():
  164.     print('Applying "Mean":')
  165.     convert_all_file_locations(single_color)
  166.     print('Done!')
  167.  
  168. # Every value on or above the threshold will be 255, everything below will be 0
  169. def threshold_routine(cut_off_point = 0.5):
  170.     print('Applying "Threshold (%s)":' % str(cut_off_point))
  171.     convert_all_file_locations(f_threshold, cut_off_point)
  172.     print('Done!')
  173.  
  174. def invert_routine(alpha = False):
  175.     print('Applying "Invert%s":' % ' (including alpha)' if alpha else ' ')
  176.     convert_all_file_locations(invert, alpha)
  177.     print('Done!')
  178.  
  179. def hue_shift_routine(amount):
  180.     print('Applying "Shift Hue (%s)":' % str(amount))
  181.     convert_all_file_locations(hue_shift, amount)
  182.     print('Done!')
  183.  
  184. def color_routine(color):
  185.     print('Applying "Color (%s)":' % str(color))
  186.     convert_all_file_locations(colorize_self, color)
  187.     print('Done!')
  188.  
  189. def grayscale_routine():
  190.     print('Applying "Grayscale":')
  191.     convert_all_file_locations(convert_image_grayscale)
  192.     print('Done!')
  193.  
  194. ############################### ACTUAL FUNCTIONS ###############################
  195.  
  196. def blend_no_alpha(img_name, args):
  197.     blend_img, alpha = args
  198.     rgba_img = Image.open(IN + '/' + img_name).convert('RGBA')
  199.     r, g, b, a = rgba_img.split()
  200.  
  201.     if (blend_img.size != rgba_img.size):
  202.         blend_img = blend_img.resize(rgba_img.size)
  203.  
  204.     r2, g2, b2, a2 = Image.blend(rgba_img, blend_img, alpha).split()
  205.     img = Image.merge('RGBA', (r2, g2, b2, a))
  206.     img.save(OUT + '/' + img_name)
  207.    
  208.     img.close()
  209.     rgba_img.close()
  210.  
  211. def rainbow(img_name, args):
  212.     rgba_img = Image.open(IN + '/' + img_name).convert('RGBA')
  213.     la_img = rgba_img.convert('LA')
  214.     l, a = la_img.split()
  215.    
  216.     colors_size = 6
  217.     colors_w = [(255, 0, 0), (255, 255, 0), (0, 255, 0), (0, 255, 255), (0, 0, 255), (255, 0, 255)]
  218.     colors_b = [(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)]
  219.  
  220.     w, h = rgba_img.size
  221.     aspect = 1
  222.     if (w != 0):
  223.         aspect = int(h / w)
  224.    
  225.     x = np.lcm(colors_size, aspect)
  226.  
  227.     l_split = [l.crop((0, w*i, w, w*(i + 1))) for i in range(0, aspect)]
  228.     a_split = [a.crop((0, w*i, w, w*(i + 1))) for i in range(0, aspect)]
  229.  
  230.     colorset = [colors_b, colors_w]
  231.  
  232.     rgb_colorized = [ImageOps.colorize(l_split[i % aspect], colorset[0][i % colors_size], colorset[1][i % colors_size]) for i in range(0, x)]
  233.     rgba_colorized = [Image.merge('RGBA', rgb_colorized[i].split() + (a_split[i % aspect],)) for i in range(0, len(rgb_colorized))]
  234.  
  235.     new_img = Image.new('RGBA', (w, w*x))
  236.     [new_img.paste(rgba_colorized[i], (0, w*i, w, w*(i+1))) for i in range(0, x)]
  237.    
  238.     new_img.save(OUT + '/' + img_name)
  239.     new_img.close()
  240.     [img.close() for img in rgba_colorized]
  241.     [img.close() for img in rgb_colorized]
  242.     la_img.close()
  243.     rgba_img.close()
  244.  
  245.     generate_mcmeta(img_name, args)
  246.  
  247. def generate_mcmeta(filename, args):
  248.     speed, inter = args
  249.     interpolate = 'true' if inter else 'false'
  250.     filepath = '%s/%s.mcmeta' % (OUT, filename)
  251.  
  252.     file_json = {}
  253.     if isfile(filepath):
  254.         with open(filepath, 'r') as file:
  255.             file_json = json.load(file)
  256.    
  257.     mcmeta_json = {'animation':{'interpolate': interpolate, 'frametime': speed}}
  258.     file_json.update(mcmeta_json)
  259.  
  260.     with open(filepath, 'w+') as file:
  261.         json.dump(file_json, file, indent=2, sort_keys=True)
  262.  
  263. def built_in_rgb(function, img_name, arg = None):
  264.     rgba_img = Image.open(IN + '/' + img_name).convert('RGBA')
  265.     r, g, b, a = rgba_img.split()
  266.     rgb_img = Image.merge('RGB', (r,g,b))
  267.    
  268.     if (arg is None):
  269.          edited_img = function(rgb_img)
  270.     else:
  271.         edited_img = function(Image.merge('RGB', (r,g,b)), arg)
  272.     r2, g2, b2 = edited_img.split()
  273.    
  274.     img = Image.merge('RGBA', (r2, g2, b2, a))
  275.     img.save(OUT + '/' + img_name)
  276.    
  277.     img.close()
  278.     edited_img.close()
  279.     rgb_img.close()
  280.     rgba_img.close()
  281.  
  282. def auto_contrast(img_name):
  283.     built_in_rgb(ImageOps.autocontrast, img_name)
  284.  
  285. def solarize(img_name, threshold = 0.5):
  286.     built_in_rgb(ImageOps.solarize, img_name, (255 * threshold))
  287.  
  288. def posterize(img_name, bits = 3):
  289.     built_in_rgb(ImageOps.posterize, img_name, bits)
  290.  
  291. def blend(img_name, args):
  292.     blend_img, alpha = args
  293.     rgba_img = Image.open(IN + '/' + img_name).convert('RGBA')
  294.  
  295.     if (blend_img.size != rgba_img.size):
  296.         blend_img = blend_img.resize(rgba_img.size)
  297.  
  298.     img = Image.blend(rgba_img, blend_img, alpha)
  299.     img.save(OUT + '/' + img_name)
  300.    
  301.     img.close()
  302.     rgba_img.close()
  303.  
  304. def colorize(img_name, colors):
  305.     rgba_img = Image.open(IN + '/' + img_name).convert('RGBA')
  306.     la_img = rgba_img.convert('LA')
  307.     l, a = la_img.split()
  308.    
  309.     black_color, white_color = colors
  310.     c_black_color = [255 * x for x in black_color]
  311.     c_white_color = [255 * x for x in white_color]
  312.  
  313.     colorized = ImageOps.colorize(l, c_black_color, c_white_color)
  314.     r, g, b = colorized.split()
  315.  
  316.     img = Image.merge('RGBA', (r, g, b, a))
  317.     img.save(OUT + '/' + img_name)
  318.  
  319.     img.close()
  320.     colorized.close()
  321.     la_img.close()
  322.     rgba_img.close()
  323.  
  324. def single_color(img_name):
  325.     rgba_img = Image.open(IN + '/' + img_name).convert('RGBA')
  326.     r, g, b, a = rgba_img.split()
  327.     rgb_img = Image.merge('RGB', (r, g, b))
  328.    
  329.     m = mean(rgb_img, a)
  330.     mean_int = [int(f) for f in m]
  331.    
  332.     sc_img = Image.new('RGB', rgba_img.size, tuple(mean_int))
  333.     r2, g2, b2 = sc_img.split()
  334.  
  335.     img = Image.merge('RGBA', (r2, g2, b2, a))
  336.     img.save(OUT + '/' + img_name)
  337.    
  338.     img.close()
  339.     sc_img.close()
  340.     rgb_img.close()
  341.     rgba_img.close()
  342.  
  343. # Calculate the mean of all pixels in an image ignoring completely transparent pixels
  344. def mean(rgb, a):
  345.     a_arr = np.array(a)
  346.     img_arr = np.array(rgb)
  347.     mask = (a_arr > 0)    # Create mask from all non-transparent pixels
  348.     ntp = img_arr[mask] # Array containing all pixels that aren't transparent
  349.     rows = len(ntp)
  350.     if rows < 1:
  351.         return (0, 0, 0)
  352.     cols = len(ntp[0])
  353.     data = np.zeros([cols, rows, 3], dtype = np.uint8)
  354.     data[:] = ntp
  355.  
  356.     c_img = Image.fromarray(data, 'RGB')
  357.     m = ImageStat.Stat(c_img).mean
  358.     c_img.close()
  359.     return m
  360.  
  361. # Note, this function doesn't apply a threshold to alpha as it isn't used much in the game.
  362. def f_threshold(img_name, cut_off_point = 0.5):
  363.     rgba_img = Image.open(IN + '/' + img_name).convert('RGBA')
  364.     rgba_array = np.array(rgba_img)
  365.  
  366.     w, h = rgba_img.size
  367.     data = np.zeros([h, w, 4], dtype = np.uint8)
  368.     r, g, b, a = init_arrays4(w, h)
  369.    
  370.     r[:] = rgba_array[..., 0]
  371.     g[:] = rgba_array[..., 1]
  372.     b[:] = rgba_array[..., 2]
  373.     a[:] = rgba_array[..., 3]
  374.  
  375.     c = 256 * cut_off_point
  376.  
  377.     r[r < c] = 0
  378.     r[r >= c] = 255
  379.     g[g < c] = 0
  380.     g[g >= c] = 255
  381.     b[b < c] = 0
  382.     b[b >= c] = 255
  383.  
  384.     data[..., 0] = r
  385.     data[..., 1] = g
  386.     data[..., 2] = b
  387.     data[..., 3] = a
  388.    
  389.     img = Image.fromarray(data, 'RGBA')
  390.    
  391.     img.save(OUT + '/' + img_name)
  392.    
  393.     img.close()
  394.     rgba_img.close()
  395.  
  396. # Colorize the image to a single color with respected luminosity
  397. def colorize_self(img_name, color):
  398.     rgba_img = Image.open(IN + '/' + img_name).convert('RGBA')
  399.     la_img = rgba_img.convert('LA')
  400.    
  401.     # Get the array representations
  402.     rgba_array = np.array(rgba_img)
  403.     gray_array = np.array(la_img)
  404.  
  405.     # Flatmap arrays on last value
  406.     flat_orig_r = np.concatenate(rgba_array[..., 0], axis = 0);
  407.     flat_orig_g = np.concatenate(rgba_array[..., 1], axis = 0);
  408.     flat_orig_b = np.concatenate(rgba_array[..., 2], axis = 0);
  409.     flat_gray = np.concatenate(gray_array[..., 0], axis = 0);
  410.  
  411.     x = max(flat_gray)
  412.     if (x != 0):
  413.         factor = max([max(flat_orig_g), max(flat_orig_b), max(flat_orig_r)]) / x
  414.     else:
  415.         factor = 0
  416.    
  417.     # Fill the data array
  418.     w, h = rgba_img.size
  419.     data = np.zeros([h, w, 4], dtype = np.uint8)
  420.     r, g, b, a = init_arrays4(w, h)
  421.    
  422.     r[:] = gray_array[..., 0] * factor * color[0]
  423.     g[:] = gray_array[..., 0] * factor * color[1]
  424.     b[:] = gray_array[..., 0] * factor * color[2]
  425.     a[:] = rgba_array[..., 3]
  426.    
  427.     data[..., 0] = r
  428.     data[..., 1] = g
  429.     data[..., 2] = b
  430.     data[..., 3] = a
  431.    
  432.     img = Image.fromarray(data, 'RGBA')
  433.     img.save(OUT + '/' + img_name)
  434.    
  435.     img.close()
  436.     la_img.close()
  437.     rgba_img.close()
  438.  
  439. def invert(img_name, invert_alpha = False):
  440.     rgba_img = Image.open(IN + '/' + img_name).convert('RGBA')
  441.     r, g, b, a = rgba_img.split()
  442.  
  443.     inverted_img = ImageOps.invert(Image.merge('RGB', (r,g,b)))
  444.     r2, g2, b2 = inverted_img.split()
  445.  
  446.     if invert_alpha:
  447.         a = a.point(lambda p: 255 - p)
  448.  
  449.     img = Image.merge('RGBA', (r2,g2,b2,a))
  450.     img.save(OUT + '/' + img_name)
  451.    
  452.     img.close()
  453.     inverted_img.close()
  454.     rgba_img.close()
  455.  
  456. # Shift the hue from r to g, from g to b, from b to r
  457. def hue_shift(img_name, amount):
  458.     rgba_img = Image.open(IN + '/' + img_name).convert('RGBA')
  459.     rgba_array = np.array(rgba_img)
  460.  
  461.     w, h = rgba_img.size
  462.     data = np.zeros([h, w, 4], dtype = np.uint8)
  463.     r, g, b, a = init_arrays4(w, h)
  464.    
  465.     r[:] = rgba_array[..., 0]
  466.     g[:] = rgba_array[..., 1]
  467.     b[:] = rgba_array[..., 2]
  468.     a[:] = rgba_array[..., 3]
  469.    
  470.     r, g, b = swap(r, g, b, amount)
  471.  
  472.     data[..., 0] = r
  473.     data[..., 1] = g
  474.     data[..., 2] = b
  475.     data[..., 3] = a
  476.    
  477.     img = Image.fromarray(data, 'RGBA')
  478.    
  479.     img.save(OUT + '/' + img_name)
  480.    
  481.     img.close()
  482.     rgba_img.close()
  483.  
  484. # Grab original image and convert to gray
  485. def convert_image_grayscale(img_name):
  486.     gray_img = Image.open(IN + '/' + img_name).convert('LA')
  487.     gray_img.save(OUT + '/' + img_name)
  488.     gray_img.close()
  489.  
  490. ################################ AUXILARY ################################
  491.  
  492. def benchmark(function, arg = None):
  493.     start_time = time.time()
  494.     if arg is None:
  495.         function()
  496.     else:
  497.         function(arg)
  498.     if (__debug__):
  499.         elapsed_time = time.time() - start_time
  500.         print_time(elapsed_time)
  501.  
  502. def print_time(elapsed_time):
  503.     millis = int(round((elapsed_time * 1000) % 1000))
  504.     seconds = int(elapsed_time) % 60
  505.     minutes = int(elapsed_time / 60) % 60
  506.     hours = int(elapsed_time / 3600)
  507.     print('Time passed: %02d:%02d:%02d.%03d' % (hours, minutes, seconds, millis))
  508.  
  509. def to_bool(s):
  510.     return (s.lower() == 'true')
  511.  
  512. def init():
  513.     if exists(OUT):
  514.         shutil.rmtree(OUT)
  515.     mdine()
  516.     subfolders = get_subfolders_rootless(IN)
  517.     [mdine(subfolder) for subfolder in subfolders]
  518.     return subfolders
  519.  
  520. def init_arrays4(w, h):
  521.     return (np.zeros([h, w]), np.zeros([h, w]), np.zeros([h, w]), np.zeros([h, w]))
  522.  
  523. def convert_all_file_locations(function, arg = None):
  524.     locations = init()
  525.     [convert_file_locations(location, function, arg) for location in locations]
  526.  
  527. def convert_all_file_locations_special(function, function_else, arg, else_arg):
  528.     locations = init()
  529.     [convert_file_locations(location, special, (function, function_else, arg, else_arg)) for location in locations]
  530.  
  531. def special(img, sp):
  532.     function, function_else, args, else_args = sp
  533.     if '/block' in img or '/item' in img:
  534.         function(img, args)
  535.     else:
  536.         function_else(img, else_args)
  537.  
  538.  
  539. def print_percentage(operation_string, meta_files, meta_action, png_files, file_action, arg = None):
  540.     pl = len(png_files)
  541.     ml = len(meta_files)
  542.     segment_count = pl + ml
  543.     toolbar_width = 50
  544.     max_len = 45
  545.     str_size = len(operation_string)
  546.     rem_size = 50 - min(str_size - 3, max_len)
  547.     str_start = max(str_size - max_len, 0)
  548.  
  549.     # setup toolbar
  550.     sys.stdout.write(' [%s]%s[%s]' % (('...' if str_start > 0 else '') + operation_string[str_start:], ' ' * rem_size, ' ' * toolbar_width))
  551.     sys.stdout.flush()
  552.     sys.stdout.write('\b' * (toolbar_width + 1)) # return to start of line, after '['
  553.  
  554.     last_write = -1
  555.     segment_writes = 0
  556.     total_segment_writes = 0
  557.    
  558.     threads = []
  559.    
  560.     for i in range(0, ml):
  561.         process = Thread(target=meta_action, args=[IN + '/' + meta_files[i], OUT + '/' + meta_files[i]])
  562.         process.start()
  563.         threads.append(process)
  564.  
  565.         segment_writes += (toolbar_width / segment_count)
  566.  
  567.         if segment_writes >= 0.999:
  568.             # update the bar
  569.             sys.stdout.write('■' * int(segment_writes))
  570.             sys.stdout.flush()
  571.             total_segment_writes += int(segment_writes)
  572.             segment_writes -= int(segment_writes)
  573.  
  574.     for i in range(0, pl):
  575.  
  576.         if (arg is None):
  577.             process = Thread(target=file_action, args=[png_files[i]])
  578.         else:
  579.             process = Thread(target=file_action, args=[png_files[i], arg])
  580.  
  581.         process.start()
  582.         threads.append(process)
  583.  
  584.         segment_writes += (toolbar_width / segment_count)
  585.  
  586.         if segment_writes >= 0.999:
  587.             # update the bar
  588.             sys.stdout.write('■' * int(segment_writes))
  589.             sys.stdout.flush()
  590.             total_segment_writes += int(segment_writes)
  591.             segment_writes -= int(segment_writes)
  592.  
  593.     for process in threads:
  594.         process.join()
  595.  
  596.     if segment_count == 0:
  597.         sys.stdout.write('■' * toolbar_width)
  598.     elif total_segment_writes < toolbar_width:
  599.         sys.stdout.write('■' * int(toolbar_width - total_segment_writes))
  600.  
  601.     sys.stdout.write(']\n') # this ends the progress bar
  602.        
  603.  
  604. def convert_file_locations(location, function, arg = None):
  605.     f, m = get_files(location)
  606.     print_percentage(IN + '/' + location, m, copyfile, f, function, arg)
  607.  
  608. def get_files(dirc):
  609.     for (dirpath, dirnames, filenames) in walk(IN + '/' + dirc):
  610.         file = [dirc + '/' + filename for filename in filenames if filename.endswith('.png')]
  611.         meta = [dirc + '/' + filename for filename in filenames if not filename.endswith('.png')]
  612.         return (file, meta)
  613.  
  614. def get_subfolders_rootless(path):
  615.     return [x[len(path) + 1::] for x in get_subfolders(path)]
  616.  
  617. def get_subfolders(path):
  618.     all_paths = []
  619.     for dirname, dirnames, filenames in walk(path):
  620.         for subdirname in dirnames:
  621.             all_paths.append(path_join(dirname, subdirname))
  622.     new_paths = [path.replace(os.sep, '/') for path in all_paths]
  623.     return new_paths
  624.  
  625. def mdine(dirc = None):
  626.     if dirc is None:
  627.         _mdine(OUT)
  628.     else:
  629.         _mdine(OUT + '/' + dirc)
  630.  
  631. def _mdine(path):
  632.     if not exists(path):
  633.         mkdir(path)
  634.  
  635. def rainbow_color_image():
  636.     stepsize = 2
  637.     data = np.zeros((32, 32, 4), dtype=np.uint8)
  638.     colors = [(255, 0, 0, 255), (255, 255, 0, 255), (0, 255, 0, 255), (0, 255, 255, 255), (0, 0, 255, 255), (255, 0, 255, 255)]
  639.     for y in range(0, 32):
  640.         for x in range(0, 32):
  641.             index = int((x + y) / stepsize) % len(colors)
  642.             data[x, y] = colors[index]
  643.     return Image.fromarray(data, 'RGBA')
  644.  
  645. def swap(arg0, arg1, arg2, amount):
  646.     if amount > 0:
  647.         temp = arg2
  648.         arg2 = arg1
  649.         arg1 = arg0
  650.         arg0 = temp
  651.         return swap(arg0, arg1, arg2, amount - 1)
  652.     else:
  653.         return arg0, arg1, arg2
  654.  
  655.  
  656. ### CALL MAIN ###
  657. main()
Advertisement
Add Comment
Please, Sign In to add comment