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
| import re import os import cv2 import turtle import numpy as np from bs4 import BeautifulSoup from win32.win32api import GetSystemMetrics
WIDTH =600 HEIGHT = 600 NUM_SAMPLE = 15
def Bezier_1(p0, p1, t): if len(p0) == 2 and len(p1) == 2: return p0[0] * (1 - t) + p1[0] * t, p0[1] * (1 - t) + p1[1] * t elif len(p0) == 1 and len(p1) == 1: return p0 * (1 - t) + p1 * t else: print('[Error]: Bezier_1 parameters error...') exit(-1)
def Bezier_2(p0, p1, p2): turtle.goto(p0) turtle.pendown() for t in range(0, NUM_SAMPLE+1): p = Bezier_1(Bezier_1(p0, p1, t/NUM_SAMPLE), Bezier_1(p1, p2, t/NUM_SAMPLE), t/NUM_SAMPLE) turtle.goto(p) turtle.penup()
def Bezier_3(p0, p1, p2, p3): p0 = -WIDTH/2 + p0[0], HEIGHT/2 - p0[1] p1 = -WIDTH/2 + p1[0], HEIGHT/2 - p1[1] p2 = -WIDTH/2 + p2[0], HEIGHT/2 - p2[1] p3 = -WIDTH/2 + p3[0], HEIGHT/2 - p3[1] turtle.goto(p0) turtle.pendown() for t in range(0, NUM_SAMPLE+1): p = Bezier_1(Bezier_1(Bezier_1(p0, p1, t/NUM_SAMPLE), Bezier_1(p1, p2, t/NUM_SAMPLE), t/NUM_SAMPLE), Bezier_1(Bezier_1(p1, p2, t/NUM_SAMPLE), Bezier_1(p2, p3, t/NUM_SAMPLE), t/NUM_SAMPLE), t/NUM_SAMPLE) turtle.goto(p) turtle.penup()
def yieldAttrs(attrs): for attr in attrs: if attr.isdigit(): yield float(attr) elif attr[0].isalpha(): yield attr[0] yield float(attr[1:]) elif attr[-1].isalpha(): yield float(attr[0: -1]) elif attr[0] == '-': yield float(attr)
def drawSVG(filename, color, is_first=True, speed=1000): svgfile = open(filename, 'r') soup = BeautifulSoup(svgfile.read(), 'lxml') height = float(soup.svg.attrs['height'][0: -2]) width = float(soup.svg.attrs['width'][0: -2]) scale = tuple(map(float, re.findall(r'scale\((.*?)\)', soup.g.attrs['transform'])[0].split(','))) scale = scale[0], -scale[1] if is_first: turtle.setup(height=height, width=width) turtle.setworldcoordinates(-width/2, 300,width-width/2, -height+300) turtle.tracer(100) turtle.pensize(1) turtle.speed(speed) turtle.penup() turtle.color(color) for path in soup.find_all('path'): attrs = path.attrs['d'].replace('\n', ' ') attrs = attrs.split(' ') attrs_yield = yieldAttrs(attrs) endl = '' for attr in attrs_yield: if attr == 'M': turtle.end_fill() x, y = attrs_yield.__next__() * scale[0], attrs_yield.__next__() * scale[1] turtle.penup() turtle.goto(-WIDTH/2+x, HEIGHT/2-y) turtle.pendown() turtle.begin_fill() elif attr == 'm': turtle.end_fill() dx, dy = attrs_yield.__next__() * scale[0], attrs_yield.__next__() * scale[1] turtle.penup() turtle.goto(turtle.xcor()+dx, turtle.ycor()-dy) turtle.pendown() turtle.begin_fill() elif attr == 'C': p1 = attrs_yield.__next__() * scale[0], attrs_yield.__next__() * scale[1] p2 = attrs_yield.__next__() * scale[0], attrs_yield.__next__() * scale[1] p3 = attrs_yield.__next__() * scale[0], attrs_yield.__next__() * scale[1] turtle.penup() p0 = turtle.xcor()+WIDTH/2, HEIGHT/2-turtle.ycor() Bezier_3(p0, p1, p2, p3) endl = attr elif attr == 'c': turtle.penup() p0 = turtle.xcor()+WIDTH/2, HEIGHT/2-turtle.ycor() p1 = attrs_yield.__next__() * scale[0] + p0[0], attrs_yield.__next__() * scale[1] + p0[1] p2 = attrs_yield.__next__() * scale[0] + p0[0], attrs_yield.__next__() * scale[1] + p0[1] p3 = attrs_yield.__next__() * scale[0] + p0[0], attrs_yield.__next__() * scale[1] + p0[1] Bezier_3(p0, p1, p2, p3) endl = attr elif attr == 'L': x, y = attrs_yield.__next__() * scale[0], attrs_yield.__next__() * scale[1] turtle.pendown() turtle.goto(-WIDTH/2+x, HEIGHT/2-y) turtle.penup() elif attr == 'l': dx, dy = attrs_yield.__next__() * scale[0], attrs_yield.__next__() * scale[1] turtle.pendown() turtle.goto(turtle.xcor()+dx, turtle.ycor()-dy) turtle.penup() endl = attr elif endl == 'C': p1 = attr * scale[0], attrs_yield.__next__() * scale[1] p2 = attrs_yield.__next__() * scale[0], attrs_yield.__next__() * scale[1] p3 = attrs_yield.__next__() * scale[0], attrs_yield.__next__() * scale[1] turtle.penup() p0 = turtle.xcor()+WIDTH/2, HEIGHT/2-turtle.ycor() Bezier_3(p0, p1, p2, p3) elif endl == 'c': turtle.penup() p0 = turtle.xcor()+WIDTH/2, HEIGHT/2-turtle.ycor() p1 = attr * scale[0] + p0[0], attrs_yield.__next__() * scale[1] + p0[1] p2 = attrs_yield.__next__() * scale[0] + p0[0], attrs_yield.__next__() * scale[1] + p0[1] p3 = attrs_yield.__next__() * scale[0] + p0[0], attrs_yield.__next__() * scale[1] + p0[1] Bezier_3(p0, p1, p2, p3) elif endl == 'L': x, y = attr * scale[0], attrs_yield.__next__() * scale[1] turtle.pendown() turtle.goto(-WIDTH/2+x, HEIGHT/2-y) turtle.penup() elif endl == 'l': dx, dy = attr * scale[0], attrs_yield.__next__() * scale[1] turtle.pendown() turtle.goto(turtle.xcor()+dx, turtle.ycor()-dy) turtle.penup() turtle.penup() turtle.update() svgfile.close()
def drawBit(img_bit, K=32): data = img_bit.reshape((-1, 3)) data = np.float32(data) criteria = (cv2.TERM_CRITERIA_EPS, 10, 1.0) compactness, labels, centers = cv2.kmeans(data, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) centers = np.uint8(centers) data_compress = centers[labels.flatten()] img_new = data_compress.reshape(img_bit.shape) count = 0 for center in centers: count += 1 print('[INFO]: Drawing %dth color...' % count) part = cv2.inRange(img_new, center, center) part = cv2.bitwise_not(part) cv2.imwrite('.tmp.bmp', part) os.system('potrace.exe .tmp.bmp -s --flat') if count == 1: drawSVG('.tmp.svg', '#%02x%02x%02x' % (center[2], center[1], center[0]), True) else: drawSVG('.tmp.svg', '#%02x%02x%02x' % (center[2], center[1], center[0]), False) os.remove('.tmp.bmp') os.remove('.tmp.svg') print('[INFO]: All done!') turtle.done()
if __name__ == '__main__': img_path = './wh.jpg' img_bit = cv2.imread(img_path) K = 32 if img_bit.shape[0] > GetSystemMetrics(1): width = int(img_bit.shape[1] * ((GetSystemMetrics(1) - 50) / img_bit.shape[0])) height = GetSystemMetrics(1) - 50 img_bit = cv2.resize(img_bit, (width, height)) drawBit(img_bit, K)
|