|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import getopt |
| 4 | +import random as r |
| 5 | +from PIL import Image |
| 6 | +from pixelsort import pixelsort |
| 7 | + |
| 8 | + |
| 9 | +def randomize_params(): |
| 10 | + angle = r.randint(90, 359) |
| 11 | + |
| 12 | + # --- DEFINE ALL PARAMETERS --- |
| 13 | + params = { |
| 14 | + 1: 'interval_function', |
| 15 | + 2: 'randomness', |
| 16 | + 3: 'lower_threshold', |
| 17 | + 4: 'upper_threshold', |
| 18 | + 5: 'sorting_function', |
| 19 | + } |
| 20 | + |
| 21 | + # --- RANDOMIZE COUNT AND CHOICE OF PARAMS --- |
| 22 | + param_count = r.randint(1, 5) |
| 23 | + selected_params = [] |
| 24 | + for _ in range(param_count): |
| 25 | + param_choice = r.randint(1, 5) |
| 26 | + if params[param_choice] not in selected_params: |
| 27 | + selected_params.append(params[param_choice]) |
| 28 | + |
| 29 | + # --- SET DEFAULTS FOR PARAMS --- |
| 30 | + args = {} |
| 31 | + args['angle'] = angle |
| 32 | + args['interval_function'] = 'threshold' |
| 33 | + args['lower_threshold'] = 0.5 |
| 34 | + args['upper_threshold'] = 0.8 |
| 35 | + args['randomness'] = 0.5 |
| 36 | + args['sorting_function'] = 'lightness' |
| 37 | + |
| 38 | + # --- UPDATE WITH RANDOMIZED VALUES --- |
| 39 | + for param in selected_params: |
| 40 | + if param == 'interval_function': |
| 41 | + interval_fns = ['random', 'threshold', 'waves'] |
| 42 | + args['interval_function'] = r.choice(interval_fns) |
| 43 | + elif param == 'randomness': |
| 44 | + args['randomness'] = r.uniform(0.5, 1) |
| 45 | + elif param == 'sorting_function': |
| 46 | + sorting_fns = ['lightness', 'hue', 'saturation', 'intensity', 'minimum'] |
| 47 | + args['sorting_function'] = r.choice(sorting_fns) |
| 48 | + elif param == 'lower_threshold': |
| 49 | + args['lower_threshold'] = r.uniform(0.5, 1) |
| 50 | + elif param == 'upper_threshold': |
| 51 | + up_thresh = r.uniform(0.6, 1) |
| 52 | + if up_thresh <= args['lower_threshold']: |
| 53 | + up_thresh += r.uniform(0.1, 1 - args['lower_threshold']) |
| 54 | + args['upper_threshold'] = up_thresh |
| 55 | + elif args['upper_threshold'] - args['lower_threshold'] < 0.25: |
| 56 | + args['lower_threshold'] -= 0.25 |
| 57 | + return args |
| 58 | + |
| 59 | + |
| 60 | +def perform_sorting(args, img): |
| 61 | + # --- PERFORM PIXELSORT WITH RANDOMIZED PARAMS --- |
| 62 | + new_img = pixelsort( |
| 63 | + image=img, |
| 64 | + angle=args['angle'], |
| 65 | + interval_function=args['interval_function'], |
| 66 | + lower_threshold=args['lower_threshold'], |
| 67 | + upper_threshold=args['upper_threshold'], |
| 68 | + randomness=args['randomness'], |
| 69 | + sorting_function=args['sorting_function'] |
| 70 | + ) |
| 71 | + return new_img |
| 72 | + |
| 73 | + |
| 74 | +def Main(): |
| 75 | + # --- DEFINE ARGS AND SET DEFAULTS --- |
| 76 | + count = 0 |
| 77 | + in_path = 'images/' |
| 78 | + out_path = 'generated/' |
| 79 | + argument_list = sys.argv[1:] |
| 80 | + options = 'hi:n:' |
| 81 | + |
| 82 | + # --- DEFINE TERMINAL ARG OPERATIONS --- |
| 83 | + try: |
| 84 | + args, _ = getopt.getopt(argument_list, options) |
| 85 | + for current_argument, current_value in args: |
| 86 | + if current_argument in ('-h'): |
| 87 | + print('-' * 30) |
| 88 | + print('-h : args description') |
| 89 | + print('-i : pass location of input img-file') |
| 90 | + print('-n : number of outputs required') |
| 91 | + print('-' * 30) |
| 92 | + if current_argument in ('-i'): |
| 93 | + print('-' * 30) |
| 94 | + in_path += current_value |
| 95 | + print(f'[+] Input-file: {in_path}') |
| 96 | + if current_argument in ('-n'): |
| 97 | + count = int(current_value) |
| 98 | + print(f'[+] Output-Count: {current_value}') |
| 99 | + print('-' * 30) |
| 100 | + |
| 101 | + except getopt.error as error: |
| 102 | + print(str(error)) |
| 103 | + |
| 104 | + # --- DELETE PREVIOUS RESULTS --- |
| 105 | + for file in os.listdir(out_path): |
| 106 | + os.remove(os.path.join(out_path, file)) |
| 107 | + |
| 108 | + # --- GENERATE 'N=COUNT' INSTANCES --- |
| 109 | + for index in range(count): |
| 110 | + |
| 111 | + # --- CALL PARAMETER FUNCTION --- |
| 112 | + args = randomize_params() |
| 113 | + |
| 114 | + # --- PRINT RANDOMIZED CHOICES --- |
| 115 | + for arg in args.items(): |
| 116 | + print(arg[0], ':', arg[1]) |
| 117 | + |
| 118 | + # --- DEFINE LOCATIONS FOR LOAD AND SAVE --- |
| 119 | + in_file = in_path |
| 120 | + out_file = out_path + f'result-0{index + 1}.png' |
| 121 | + img = Image.open(in_file) |
| 122 | + |
| 123 | + # --- CALL SORT FUNCTION --- |
| 124 | + new_img = perform_sorting(args, img) |
| 125 | + # --- SAVE NEW FILE --- |
| 126 | + new_img.save(out_file) |
| 127 | + print('-' * 30) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + Main() |
0 commit comments