Implemented parallel processing amongst render and optimization jobs. This thing is much much faster now

This commit is contained in:
mmcwilliams 2023-11-30 15:05:26 -05:00
parent 1888bf9aba
commit 4c8a5400e4
1 changed files with 40 additions and 11 deletions

View File

@ -63,6 +63,7 @@ class Posterize:
self.determine_colors()
self.stipple()
self.preview()
self.optimize()
def posterize (self):
lab = cv2.cvtColor(self.image, cv2.COLOR_BGR2LAB)
@ -185,21 +186,29 @@ class Posterize:
'output_svg' : output_svg
})
if self.jobs > 1 :
print(f'Running {self.jobs} processes simultaneously for {len(cmds)} jobs')
if self.jobs > 1 and len(cmds) > 1:
print(f'Running {self.jobs} stipple_gen processes simultaneously for {len(cmds)} jobs')
results = []
pool = ThreadPool(self.jobs)
for job in cmds :
pool.apply_async(self.render, (job,))
pool.close()
pool.join()
result = pool.apply_async(self.render, (job,))
results.append(result)
[result.wait() for result in results]
else :
for job in cmds :
self.render(job)
def render (self, job) :
subprocess.Popen(job['cmd'], cwd = self.stipple_gen)
name = os.path.basename(job['output_svg'])
print(f'Starting stipple_gen {name}...')
proc = subprocess.Popen(job['cmd'], cwd = self.stipple_gen, stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if not line:
break
l = line.decode('ascii').strip()
print(f'[{name}] {l}')
self.svgs.append(job['output_svg'])
self.previews.append({
'layer' : job['output_image'],
@ -220,11 +229,31 @@ class Posterize:
self.show(composite)
for svg in self.svgs :
cmd = [ 'svgopt', svg, svg]
print(cmd)
subprocess.call(cmd)
def optimize (self) :
if self.jobs > 1 and len(self.svgs) > 1:
print(f'Running {self.jobs} svgopt processes simultaneously for {len(self.svgs)} jobs')
results = []
pool = ThreadPool(self.jobs)
for svg in self.svgs:
result = pool.apply_async(self.svgopt, (svg,))
results.append(result)
[result.wait() for result in results]
else :
for svg in self.svgs :
self.svgopt(svg)
def svgopt (self, svg) :
name = os.path.basename(svg)
cmd = [ 'svgopt', svg, svg]
print(f'Optimizing {name}...')
proc = subprocess.Popen(cmd, cwd = self.stipple_gen, stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if not line:
break
l = line.decode('ascii').strip()
print(f'[{name}] {l}')
def show (self, mat) :
if not self.headless :
cv2.imshow('image', mat)