From 1888bf9abab7f2d7f4a7075b54708c524cd76e5a Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Thu, 30 Nov 2023 13:15:58 -0500 Subject: [PATCH] Completely boneheaded move: removed the poorly-working color matching logic completely rather than waiting for the rewrite to replace. Restored that and added threading logic for running renders in parallel --- py/posterize.py | 58 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/py/posterize.py b/py/posterize.py index f8f7689..077bc51 100644 --- a/py/posterize.py +++ b/py/posterize.py @@ -5,6 +5,7 @@ import cv2 from common import convert_color, closest_color_weighted_euclidean, closest_color_euclidean, create_colored_image, remove_from_list, list_match, to_luma, convertScale import os import subprocess +from multiprocessing.pool import ThreadPool from functools import cmp_to_key def sort_pallete (a, b): @@ -101,7 +102,28 @@ class Posterize: 'space' : self.pallete_space }) - + for i in range(self.n_colors) : + if list_match(self.original_colors[i], white) : + continue + original = self.original_colors[i] #BGR + mask = self.extract_color_mask(self.image, original) + original_normalized = convert_color(original, 'BGR', self.pallete_space) + if self.pallete_space == 'RGB' or self.pallete_space == 'BGR' : + closest, dist = closest_color_weighted_euclidean(self.colors, original_normalized, self.pallete_space) + else : + closest, dist = closest_color_euclidean(self.colors, original_normalized) + self.colors = remove_from_list(self.colors, closest) + name = self.match_color_name(closest) + layer_name = f'{name}.png' + output_layer = os.path.join(self.output, layer_name) + cv2.imwrite(output_layer, mask) + self.layers.append({ + 'layer' : output_layer, + 'color' : closest, + 'space' : self.pallete_space + }) + mask = cv2.bitwise_not(mask) + composite[mask > 0] = np.array(closest) composite_name = f'posterized.png' composite_path = os.path.join(self.output, composite_name) @@ -131,6 +153,8 @@ class Posterize: def stipple (self) : sanity_check = 0 + cmds = [] + for layer in self.layers : if 'WHITE.png' in layer['layer'] : continue @@ -154,14 +178,34 @@ class Posterize: '--config', self.conf, '--maxParticles', str(max_particles) ] - print(cmd) - subprocess.call(cmd, cwd = self.stipple_gen) - self.svgs.append(output_svg) - self.previews.append({ - 'layer' : output_image, - 'color' : layer['color'] + cmds.append({ + 'cmd' : cmd, + 'color' : layer['color'], + 'output_image' : output_image, + 'output_svg' : output_svg }) + if self.jobs > 1 : + print(f'Running {self.jobs} processes simultaneously for {len(cmds)} jobs') + pool = ThreadPool(self.jobs) + for job in cmds : + pool.apply_async(self.render, (job,)) + pool.close() + pool.join() + + else : + for job in cmds : + self.render(job) + + + def render (self, job) : + subprocess.Popen(job['cmd'], cwd = self.stipple_gen) + self.svgs.append(job['output_svg']) + self.previews.append({ + 'layer' : job['output_image'], + 'color' : job['color'] + }) + def preview (self) : composite = create_colored_image(self.w, self.h, [255, 255, 255]) for layer in self.previews :