-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_drawing.py
231 lines (193 loc) · 9.73 KB
/
gen_drawing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'''
MIT License
Copyright (c) 2021
Michal Adamik, Jozef Goga, Jarmila Pavlovicova, Andrej Babinec, Ivan Sekaj
Faculty of Electrical Engineering and Information Technology
of the Slovak University of Technology in Bratislava
Ilkovicova 3, 812 19 Bratislava 1, Slovak Republic
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
# import libraries
import time
import datetime
import sys, os
import numpy as np
import matplotlib.pyplot as plt
# add a folder with a library to the path
sys.path.append(".")
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "./genetic")
# import functions from the genetic library
from genetic.utils import *
from PIL import Image, ImageDraw, ImageOps
from image4layer import Image4Layer
# ------------------------------ USER PARAMETERS --------------------------
img_str = "Darwin_enhanced.jpg" # image to load (from ./images folder)
basewidth = 256 # output width of generated image
deterministic_mode = True # reproducible results [True, False]
generate_gif = True # generate animation [True, False]
deterministic_seed = 42 # seed for pseudo-random generator
N = 6000 # number of objects in created image
# --- Genetic Optimization ---
NEvo = 10 # number of evolution steps per one optimized object
MAX_BUFF = 5 # stopping evolution if there are no changes (MAX_BUFF consecutive evolution steps)
MAX_ADDMUT = 5 # [%] - maximum aditive mutation range
MUT_RATE = 20 # [%] - mutation rate (percentage of chromosomes to be mutated)
LINE_WIDTH = 2 # [px] line width
MLTPL_EVO_PARAMS = 1 # parameter multiplier
BLEND_MODE = "darken" # available options: ["normal", "multiply", "screen", "overlay", "darken", "lighten", "color_dodge", "color_burn", "hard_light", "soft_light", "difference", "exclusion", "hue", "saturation", "color", "luminosity", "vivid_light", "pin_light", "linear_dodge", "subtract"]
# -------------------------------------------------------------------------
"""
Optimized parameters: x1,x2,y1,y2 (for one line)
-------> y
| °°°°°°°°°°°°°°°
| ° °
| ° °
v ° °
x ° °
°°°°°°°°°°°°°°°
x1,x2 <0, image_height> - vector of x positions
y1,y2 <0, image_width> - vector of y positions
"""
'''
NOTE:
- numpy -> works with the image as a dimensional tensor (H, W, D)
[ the higher axis designation and the tensor correspond to this (x,y,d) ]
- Pillow -> works with the image as a dimensional tensor (W, H, D)
[ this results in a discrepancy and a change of labeling, x and y in code according to object type ]
'''
# if deterministic mode, use specified seed for reproducible results
if (deterministic_mode):
np.random.seed(deterministic_seed)
# rendering settings (font and style)
plt.style.use('seaborn-paper')
plt.rcParams['font.family'] = 'serif'
plt.rcParams['axes.linewidth'] = 0.1 # frame boundaries in graphs
# load image and convert it to greyscale
orig_img = Image.open("./images/" + img_str).convert('L')
# resize image to specified width with aspect ratio preserved
wpercent = (basewidth/float(orig_img.size[0]))
hsize = int((float(orig_img.size[1])*float(wpercent)))
orig_img = orig_img.resize((basewidth,hsize), Image.ANTIALIAS)
# convert to numpy array
orig_img = np.asarray(orig_img, dtype=int)
# start the timer
start_time = time.time()
# --------------------------------------------------------
# generate empty image with background colour
gen_img = Image.new('RGBA', (orig_img.shape[1], orig_img.shape[0]), COLOUR_WHITE)
gen_img = gen_img.convert('L') # canvas
# definition of search space limitations (for one line segment only)
OneSpace = np.concatenate((np.zeros((1,4)), # mininum
np.array([[orig_img.shape[0]-1, orig_img.shape[0]-1, orig_img.shape[1]-1, orig_img.shape[1]-1]])), axis=0) # maximum
# range of changes for the additive mutation
Amp = OneSpace[1,:]*(MAX_ADDMUT/100.0)
# results to be saved
lpoly = np.zeros((N,6)) # (x1,x2,y1,y2,stroke,fitness)
data = list() # list of fitness values
# we start from the white canvas to which we add line segments
rfit = None # initial fitness value
buffer = 0 # auxiliary variable to stop evolution if no changes occur
count = 1 # number of objects in final image
images = [] # list of image used for animation process
if generate_gif:
images.append(gen_img)
# repeat, until we reached specified number of line segments
while(count<=N):
# initial population generation
NewPop = genLinespop(24*MLTPL_EVO_PARAMS, Amp, OneSpace)
# first fitness evaluation
fitness = evalFitness(NewPop, orig_img, gen_img, rfit, LINE_WIDTH, BLEND_MODE)
# start of genetic optimization process
for i in range(NEvo): # high enough value (we expect an early stop)
OldPop = np.copy(NewPop) # save population and fitness from previous generation
fitnessOld = np.copy(fitness)
PartNewPop1, PartNewFit1 = selbest(OldPop, fitness, [3*MLTPL_EVO_PARAMS,2*MLTPL_EVO_PARAMS,1*MLTPL_EVO_PARAMS]) # select best lines
PartNewPop2, PartNewFit2 = selsus(OldPop, fitness, 18*MLTPL_EVO_PARAMS)
PartNewPop2 = mutLine(PartNewPop2, MUT_RATE/100.0, Amp, OneSpace) # additive mutation
NewPop = np.concatenate((PartNewPop1, PartNewPop2), axis=0) # create new population
fitness = evalFitness(NewPop, orig_img, gen_img, rfit, LINE_WIDTH, BLEND_MODE)
if (np.min(fitness) == np.min(fitnessOld)):
buffer += 1 # if we stagnate start with counting
else:
buffer = 0 # if the solution has improved, continue evolution
# if we have exceeded the maximum limit, we will stop evolution
if (buffer >= MAX_BUFF):
break
# add the best line segment in the image and continue evolution
psol, rfitnew = selbest(NewPop, fitness, [1])
if(rfit is None):
rfit = 1e6 # safe big value
# draw line segment only if it improves fitness
if(rfitnew < rfit):
rfit = rfitnew
data.append(rfit) # save line segment info
minX = int(np.min([psol[0,0],psol[0,1]]))
maxX = int(np.max([psol[0,0],psol[0,1]]))
deltaX = int(maxX - minX) + 1
minY = int(np.min([psol[0,2],psol[0,3]]))
maxY = int(np.max([psol[0,2],psol[0,3]]))
deltaY = int(maxY - minY) + 1
draw = Image.new('RGBA', (deltaY, deltaX), (255,255,255,0))
draw = draw.convert('L')
pdraw = ImageDraw.Draw(draw)
p = ((int(psol[0,2])-minY, int(psol[0,0])-minX),(int(psol[0,3])-minY, int(psol[0,1])-minX))
mask_img = Image.new('1', (draw.size[0], draw.size[1]), 0)
ImageDraw.Draw(mask_img).line(p, fill=1, width=LINE_WIDTH)
mask = np.array(mask_img)
tgrey = orig_img[minX:minX+deltaX, minY:minY+deltaY] * mask
tgrey = tgrey[tgrey != 0]
# compute the lightest shade of the line segment
c = int(np.max(tgrey))
# create new line segment
pdraw.line(p, fill=(c), width=LINE_WIDTH)
partgenImg = gen_img.crop((minY, minX, minY + deltaY, minX + deltaX))
# call blending mode function by name
out = eval('Image4Layer.' + BLEND_MODE)(draw, partgenImg)
gen_img.paste(out, (minY, minX))
print("# " + str(count) + " Fitness: " + str(rfit))
lpoly[count-1,:] = np.concatenate(np.array((int(psol[0,2]), int(psol[0,3]), int(psol[0,0]), int(psol[0,1]), 255-c, rfit[0])).reshape(6,1))
count += 1 # increment counter of drawn line segments
if generate_gif:
images.append(gen_img.convert('P'))
# create new graph
fig, ax = plt.subplots()
plt.plot(data, 'b', linewidth=0.5)
plt.title('Image vectorization via genetic evolution')
plt.xlabel('Number of generations')
plt.ylabel('Fitness')
plt.xlim(left=0)
# grid and display settings
plt.box(True)
ax.set_axisbelow(True)
ax.minorticks_on()
ax.grid(which='major', linestyle='--', linewidth='0.5')
ax.grid(which='minor', linestyle='-.', linewidth='0.05', alpha=0.1)
# display the resulting graph and list the solution found
plt.show()
# find out the final solution
sol, rfit = selbest(NewPop, fitness, [1])
print("Final fitness value: " + str(rfit[0]))
print("--- Evolution lasted: %s seconds ---" % (time.time() - start_time))
# save generated images
uniq_filename = str(datetime.datetime.now().date()) + '_' + str(datetime.datetime.now().time()).replace(':', '.')
out_path = u"./results/{}.png".format(img_str.rsplit('.', 1)[0] + '_' + uniq_filename)
gen_img.save(out_path, dpi=(600,600))
# save solution info to csv file
np.savetxt("./results/" + img_str.rsplit('.', 1)[0] + '_' + uniq_filename + ".csv", lpoly, delimiter=";")
# save the animation
if generate_gif:
images[0].save(u"./results/{}.gif".format(img_str.rsplit('.', 1)[0] + '_' + uniq_filename), save_all=True, append_images=images[1::10], optimize=False, duration=2, loop=0)