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

This commit is contained in:
mmcwilliams 2023-11-30 13:15:58 -05:00
parent 9891be61a8
commit 1888bf9aba
1 changed files with 51 additions and 7 deletions

View File

@ -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 :