```class QuadArt:
...
def recursive_draw(self, x, y, w, h):
'''
if self.too_many_colors(int(x), int(y), int(w), int(h)):
self.recursive_draw(x,         y,         w/2.0, h/2.0)
self.recursive_draw(x + w/2.0, y,         w/2.0, h/2.0)
self.recursive_draw(x,         y + h/2.0, w/2.0, h/2.0)
self.recursive_draw(x + w/2.0, y + h/2.0, w/2.0, h/2.0)
else:
self.draw_avg(x, y, w, h)```

```def recursive_draw(self, x, y, w, h):
'''
if self.too_many_colors(int(x), int(y), int(w), int(h)):
self.recurse_depth += 1

self.recursive_draw(x,         y,         w/2.0, h/2.0)
self.recursive_draw(x + w/2.0, y,         w/2.0, h/2.0)
self.recursive_draw(x,         y + h/2.0, w/2.0, h/2.0)
self.recursive_draw(x + w/2.0, y + h/2.0, w/2.0, h/2.0)

self.recurse_depth -= 1

if self.recurse_depth == 3:
else:
self.draw_avg(x, y, w, h)

```def loading_bar(recurse_depth):
global start_time
try:
start_time
except:
start_time = time.time()
cur_time = time.time()
- cur_time + start_time
+ '] ' \
+ 'time left: {} secs'.format(int(time_left)).ljust(19) \
+ '\r', end='')```

```import imageio
import numpy as np```

`img = imageio.imread(filename)`

`self.img = self.img[up:down,left:right]`

```input_width = img.shape[1]
input_height = img.shape[0]```

```if input_width < input_height:
difference = input_height - input_width
subtract_top = int(difference/2)
subtract_bot = difference - subtract_top
img = img[subtract_top:-subtract_bot,:]
elif input_height < input_width:
difference = input_width - input_height
subtract_left = int(difference/2)
subtract_right = difference - subtract_left
img = img[:,subtract_left:-subtract_right]```

```# Selecting colors
red = img[:,:,0]
green = img[:,:,1]
blue = img[:,:,2]
# Calculating averages from colors
red_avg = np.average(red)
green_avg = np.average(green)
blue_avg = np.average(blue)
# Calculating standard deviations from colors
red_std = np.std(red)
green_std = np.std(green)
blue_std = np.std(blue)```

```class QuadArt:
...
def too_many_colors(self, x, y, w, h):
if w * self.output_scale <= 2 or w <= 2:
return False
img = self.img[y:y+h,x:x+w]
red = img[:,:,0]
green = img[:,:,1]
blue = img[:,:,2]

red_avg = np.average(red)
green_avg = np.average(green)
blue_avg = np.average(blue)

if red_avg >= 254 and green_avg >= 254 and blue_avg >= 254:
return False

if 255 - red_avg < self.std_thresh and 255 - green_avg < self.std_thresh \
and 255 - blue_avg < self.std_thresh:
return True

red_std = np.std(red)
if red_std > self.std_thresh:
return True

green_std = np.std(green)
if green_std > self.std_thresh:
return True

blue_std = np.std(blue)
if blue_std > self.std_thresh:
return True

return False```

1. 选择颜色
2. 从颜色计算平均值
3. False如果平均值非常接近白色，则立即返回
4. 计算颜色的标准偏差
5. True如果标准偏差大于任何颜色的阈值，则返回（进一步递归）
6. 否则返回 False

```# Import Wand
from wand.image import Image
from wand.display import Display
from wand.color import Color
from wand.drawing import Drawing

# Set up canvas to draw on
canvas = Image(width = output_size,
height = output_size,
background = Color('white'))
canvas.format = 'png'
draw = Drawing()

# Draw circles and rectangles and anything else here
draw.fill_color = Color('rgb(%s,%s,%s)' % (red, green, blue))
draw.circle((x_center, y_center), (x_edge, y_edge))
draw.rectangle(x, y, x + w, y + h)

# Write drawing to the canvas
draw(canvas)

# If you want to display image to the screen
display(canvas)

# If you want to save image to a file
canvas.save(filename='output.png')```

`output_scale = float(output_size) / input_width`

```class QuadArt:
...
def draw_avg(self, x, y, w, h):
avg_color = self.get_color(int(x), int(y), int(w), int(h))
self.draw_in_box(avg_color, x, y, w, h)```

```class QuadArt:
...
def get_color(self, x, y, w, h):
img = self.img[y : y + h,
x : x + w]
red = np.average(img[:,:,0])
green = np.average(img[:,:,1])
blue = np.average(img[:,:,2])
color = Color('rgb(%s,%s,%s)' % (red, green, blue))
return color```

```class QuadArt:
...
def draw_in_box(self, color, x, y, w, h):
x *= self.output_scale
y *= self.output_scale
w *= self.output_scale
h *= self.output_scale

self.draw.fill_color = color

if self.draw_type == 'circle':
self.draw.circle((int(x + w/2.0), int(y + h/2.0)),
(int(x + w/2.0), int(y)))
else:
self.draw.rectangle(x, y, x + w, y + h)```

0 条评论