From 14d80a306bb8a5d28aa76b62e78476976f9f5025 Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Wed, 29 Nov 2023 01:34:47 -0500 Subject: [PATCH] use a shared conf file for rendering to svg --- py/conf/base.conf | 12 ++++++++++++ py/posterize.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 py/conf/base.conf diff --git a/py/conf/base.conf b/py/conf/base.conf new file mode 100644 index 0000000..feb5538 --- /dev/null +++ b/py/conf/base.conf @@ -0,0 +1,12 @@ +canvasWidth=1301 +canvasHeight=914 +windowWidth=1301 +windowHeight=914 +dotSize=6 +maxGenerations=50 +maxParticles=2500 +minDotSize=1.8 +line=1.8 +fill=true +mode="stipple" +display=true \ No newline at end of file diff --git a/py/posterize.py b/py/posterize.py index 964a664..1693d3e 100644 --- a/py/posterize.py +++ b/py/posterize.py @@ -4,6 +4,7 @@ import argparse import cv2 from common import convert_color, closest_color_weighted_euclidean, closest_color_euclidean, create_colored_image, remove_from_list, list_match import os +import subprocess class Posterize: """Posterize an image and then find nearest colors to use""" @@ -22,6 +23,7 @@ class Posterize: h = 0 w = 0 n_colors = 3 + max_particles = 3000 white = [255, 255, 255] @@ -41,6 +43,7 @@ class Posterize: self.flatten_pallete() self.posterize() self.determine_colors() + self.ratio() def posterize (self): lab = cv2.cvtColor(self.image, cv2.COLOR_BGR2LAB) @@ -71,6 +74,16 @@ class Posterize: blank = create_colored_image(self.w, self.h, [255, 255, 255]) composite = create_colored_image(self.w, self.h, [255, 255, 255]) + mask = self.extract_color_mask(self.image, white) + layer_name = f'WHITE.png' + output_layer = os.path.join(self.output, layer_name) + cv2.imwrite(output_layer, mask) + self.layers.append({ + 'layer' : output_layer, + 'color' : white, + 'space' : self.pallete_space + }) + for i in range(self.n_colors) : if list_match(self.original_colors[i], white) : continue @@ -114,3 +127,19 @@ class Posterize: def match_color_name (self, key) : return self.colors_dict[f'{key[0]},{key[1]},{key[2]}'] + def ratio (self) : + sanity_check = 0 + for layer in self.layers : + if 'WHITE.png' in layer['layer'] : + continue + l = cv2.imread(layer['layer'], 0) + (h, w) = l.shape[:2] + total = h * w + black = total - cv2.countNonZero(l) + ratio = black/total + max_particles = round(ratio * self.max_particles) + print(layer['layer']) + print(max_particles) + + +