2023-10-21 12:26:58 +00:00
|
|
|
from sklearn.cluster import MiniBatchKMeans
|
|
|
|
import numpy as np
|
|
|
|
import argparse
|
|
|
|
import cv2
|
2023-11-30 04:08:23 +00:00
|
|
|
from common import convert_color, closest_color_weighted_euclidean, closest_color_euclidean, create_colored_image, remove_from_list, list_match, to_luma, convertScale
|
2023-11-25 14:40:13 +00:00
|
|
|
import os
|
2023-11-29 06:34:47 +00:00
|
|
|
import subprocess
|
2023-11-30 18:15:58 +00:00
|
|
|
from multiprocessing.pool import ThreadPool
|
2023-11-30 04:08:23 +00:00
|
|
|
from functools import cmp_to_key
|
|
|
|
|
|
|
|
def sort_pallete (a, b):
|
|
|
|
A = to_luma(a, 'BGR')
|
|
|
|
B = to_luma(b, 'BGR')
|
|
|
|
if A > B :
|
|
|
|
return 1
|
|
|
|
if B < A :
|
|
|
|
return -1
|
|
|
|
return 0
|
2023-10-21 12:26:58 +00:00
|
|
|
|
|
|
|
class Posterize:
|
|
|
|
"""Posterize an image and then find nearest colors to use"""
|
|
|
|
colors = []
|
2023-10-25 00:44:09 +00:00
|
|
|
colors_dict = {}
|
2023-10-22 00:27:30 +00:00
|
|
|
original_colors = []
|
2023-11-25 14:40:13 +00:00
|
|
|
layers = []
|
2023-11-29 12:53:17 +00:00
|
|
|
previews = []
|
2023-11-30 04:08:23 +00:00
|
|
|
svgs = []
|
|
|
|
headless = False
|
2023-11-30 13:09:53 +00:00
|
|
|
jobs = 1
|
2023-10-25 00:44:09 +00:00
|
|
|
|
2023-10-21 17:51:38 +00:00
|
|
|
pallete = None
|
2023-10-25 00:44:09 +00:00
|
|
|
pallete_space = 'BGR'
|
|
|
|
comparison_space = 'BGR'
|
|
|
|
|
|
|
|
image = None
|
|
|
|
|
2023-10-21 17:51:38 +00:00
|
|
|
h = 0
|
|
|
|
w = 0
|
|
|
|
n_colors = 3
|
2023-11-30 04:08:23 +00:00
|
|
|
max_particles = 17000
|
2023-11-29 12:53:17 +00:00
|
|
|
conf = os.path.abspath('./conf/base.conf')
|
2023-11-30 04:08:23 +00:00
|
|
|
stipple_gen = os.path.abspath('../../stipple_gen')
|
2023-10-25 00:44:09 +00:00
|
|
|
|
2023-10-22 00:27:30 +00:00
|
|
|
white = [255, 255, 255]
|
2023-10-21 17:51:38 +00:00
|
|
|
|
2023-11-25 14:40:13 +00:00
|
|
|
output = None
|
2023-10-25 00:44:09 +00:00
|
|
|
|
2023-11-30 13:09:53 +00:00
|
|
|
def __init__ (self, image, pallete, n_colors, output, headless, jobs) :
|
2023-10-21 17:51:38 +00:00
|
|
|
self.image = cv2.imread(image)
|
|
|
|
(self.h, self.w) = self.image.shape[:2]
|
|
|
|
self.pallete = pallete
|
2023-10-22 00:27:30 +00:00
|
|
|
self.n_colors = n_colors + 1
|
2023-10-25 00:44:09 +00:00
|
|
|
self.output = output
|
2023-11-30 04:08:23 +00:00
|
|
|
self.headless = headless
|
2023-11-30 13:09:53 +00:00
|
|
|
self.jobs = jobs
|
2023-11-25 14:40:13 +00:00
|
|
|
|
|
|
|
if not os.path.exists(self.output) :
|
|
|
|
print(f'Output directory {self.output} does not exist, creating...')
|
|
|
|
os.makedirs(self.output)
|
2023-10-22 00:27:30 +00:00
|
|
|
|
2023-10-25 00:44:09 +00:00
|
|
|
self.flatten_pallete()
|
2023-10-22 00:27:30 +00:00
|
|
|
self.posterize()
|
|
|
|
self.determine_colors()
|
2023-11-29 12:53:17 +00:00
|
|
|
self.stipple()
|
|
|
|
self.preview()
|
2023-10-21 17:51:38 +00:00
|
|
|
|
2023-10-22 00:27:30 +00:00
|
|
|
def posterize (self):
|
2023-10-21 17:51:38 +00:00
|
|
|
lab = cv2.cvtColor(self.image, cv2.COLOR_BGR2LAB)
|
|
|
|
feature = lab.reshape((self.h * self.w, 3))
|
|
|
|
|
|
|
|
clusters = MiniBatchKMeans(n_clusters = self.n_colors, n_init = 'auto')
|
|
|
|
|
|
|
|
labels = clusters.fit_predict(feature)
|
|
|
|
quant = clusters.cluster_centers_.astype('uint8')[labels]
|
2023-10-21 12:26:58 +00:00
|
|
|
|
2023-10-21 17:51:38 +00:00
|
|
|
rquant = quant.reshape((self.h, self.w, 3))
|
|
|
|
rfeature = feature.reshape((self.h, self.w, 3))
|
|
|
|
|
|
|
|
bgrquant = cv2.cvtColor(rquant, cv2.COLOR_LAB2BGR)
|
2023-10-22 00:27:30 +00:00
|
|
|
#bgrfeature = cv2.cvtColor(rfeature, cv2.COLOR_LAB2BGR)
|
|
|
|
|
|
|
|
self.image = bgrquant
|
|
|
|
|
2023-11-30 04:08:23 +00:00
|
|
|
self.show(bgrquant)
|
2023-10-21 17:51:38 +00:00
|
|
|
|
2023-10-22 00:27:30 +00:00
|
|
|
def determine_colors (self):
|
|
|
|
reshaped = self.image.reshape(-1, self.image.shape[2])
|
|
|
|
self.original_colors = np.unique(reshaped, axis=0)
|
2023-11-25 14:40:13 +00:00
|
|
|
white, white_dist = closest_color_weighted_euclidean(self.original_colors, [255, 255, 255], 'BGR')
|
2023-10-25 00:44:09 +00:00
|
|
|
|
|
|
|
blank = create_colored_image(self.w, self.h, [255, 255, 255])
|
2023-10-25 01:01:09 +00:00
|
|
|
composite = create_colored_image(self.w, self.h, [255, 255, 255])
|
2023-11-25 14:40:13 +00:00
|
|
|
|
2023-11-29 06:34:47 +00:00
|
|
|
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)
|
2023-11-30 13:09:53 +00:00
|
|
|
|
2023-11-29 06:34:47 +00:00
|
|
|
self.layers.append({
|
|
|
|
'layer' : output_layer,
|
|
|
|
'color' : white,
|
|
|
|
'space' : self.pallete_space
|
|
|
|
})
|
|
|
|
|
2023-11-30 18:15:58 +00:00
|
|
|
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)
|
2023-11-30 04:08:23 +00:00
|
|
|
|
|
|
|
composite_name = f'posterized.png'
|
|
|
|
composite_path = os.path.join(self.output, composite_name)
|
|
|
|
|
|
|
|
cv2.imwrite(composite_path, composite)
|
2023-10-25 00:44:09 +00:00
|
|
|
|
2023-11-30 04:08:23 +00:00
|
|
|
self.show(composite)
|
2023-10-22 00:27:30 +00:00
|
|
|
|
2023-10-24 02:05:58 +00:00
|
|
|
def extract_color_mask (self, image, color):
|
2023-10-22 00:27:30 +00:00
|
|
|
mask = cv2.inRange(image, color, color)
|
|
|
|
return cv2.bitwise_not(mask)
|
2023-10-25 00:44:09 +00:00
|
|
|
|
|
|
|
def flatten_pallete (self) :
|
|
|
|
for color in self.pallete.colors:
|
|
|
|
self.colors.append(color['color'])
|
2023-11-30 04:08:23 +00:00
|
|
|
self.colors_dict[f"{color['color'][0]},{color['color'][1]},{color['color'][2]}"] = color['name']
|
2023-10-25 00:44:09 +00:00
|
|
|
self.pallete_space = color['space']
|
2023-11-30 04:08:23 +00:00
|
|
|
|
|
|
|
#self.colors = sorted(self.colors, key=cmp_to_key(sort_pallete))
|
|
|
|
#for color in self.colors :
|
|
|
|
# print(to_luma(color, self.pallete_space))
|
|
|
|
|
|
|
|
#quit()
|
2023-10-25 00:44:09 +00:00
|
|
|
|
|
|
|
def match_color_name (self, key) :
|
|
|
|
return self.colors_dict[f'{key[0]},{key[1]},{key[2]}']
|
2023-11-25 14:40:13 +00:00
|
|
|
|
2023-11-29 12:53:17 +00:00
|
|
|
def stipple (self) :
|
2023-11-29 06:34:47 +00:00
|
|
|
sanity_check = 0
|
2023-11-30 18:15:58 +00:00
|
|
|
cmds = []
|
|
|
|
|
2023-11-29 06:34:47 +00:00
|
|
|
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)
|
2023-11-29 12:53:17 +00:00
|
|
|
input_image = os.path.abspath(layer['layer'])
|
2023-11-29 23:33:02 +00:00
|
|
|
dir_name, file_name = os.path.split(input_image)
|
2023-11-29 12:53:17 +00:00
|
|
|
file_part, ext = os.path.splitext(file_name)
|
|
|
|
output_image = os.path.join(dir_name, f'{file_part}_preview.png')
|
|
|
|
output_svg = os.path.join(dir_name, f'{file_part}.svg')
|
|
|
|
cmd = [
|
|
|
|
'bash',
|
|
|
|
'stipple_gen.sh',
|
|
|
|
'--inputImage', input_image,
|
|
|
|
'--outputImage', output_image,
|
|
|
|
'--outputSVG', output_svg,
|
|
|
|
'--config', self.conf,
|
|
|
|
'--maxParticles', str(max_particles)
|
|
|
|
]
|
2023-11-30 18:15:58 +00:00
|
|
|
cmds.append({
|
|
|
|
'cmd' : cmd,
|
|
|
|
'color' : layer['color'],
|
|
|
|
'output_image' : output_image,
|
|
|
|
'output_svg' : output_svg
|
2023-11-30 04:08:23 +00:00
|
|
|
})
|
2023-11-29 12:53:17 +00:00
|
|
|
|
2023-11-30 18:15:58 +00:00
|
|
|
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']
|
|
|
|
})
|
|
|
|
|
2023-11-29 12:53:17 +00:00
|
|
|
def preview (self) :
|
2023-11-30 04:08:23 +00:00
|
|
|
composite = create_colored_image(self.w, self.h, [255, 255, 255])
|
|
|
|
for layer in self.previews :
|
|
|
|
l = cv2.imread(layer['layer'], 0)
|
|
|
|
mask = cv2.bitwise_not(l)
|
|
|
|
composite[mask > 0] = np.array(convert_color(layer['color'], self.pallete_space, 'BGR'))
|
|
|
|
|
|
|
|
composite_name = f'preview.png'
|
|
|
|
composite_path = os.path.join(self.output, composite_name)
|
2023-11-29 06:34:47 +00:00
|
|
|
|
2023-11-30 04:08:23 +00:00
|
|
|
cv2.imwrite(composite_path, composite)
|
|
|
|
|
|
|
|
self.show(composite)
|
|
|
|
|
|
|
|
for svg in self.svgs :
|
|
|
|
cmd = [ 'svgopt', svg, svg]
|
|
|
|
print(cmd)
|
|
|
|
subprocess.call(cmd)
|
2023-11-29 06:34:47 +00:00
|
|
|
|
2023-11-30 04:08:23 +00:00
|
|
|
def show (self, mat) :
|
|
|
|
if not self.headless :
|
|
|
|
cv2.imshow('image', mat)
|
|
|
|
cv2.waitKey(0)
|
|
|
|
cv2.destroyAllWindows()
|