def draw_style_text(svg): text = '怡红快绿' styleDict = {} # styleDict['fill'] = 'black' styleDict['font-family'] = 'Microsoft YaHei' styleDict['font-size'] = '50px' styleList = get_styles(styleDict) W, H = svg.get_size() svg.draw(add_style('text', styleList)) xInter = 60 yInter = 60 x0 = (W - xInter) / 2 y0 = (H - yInter) / 2 theta = 0 w, h = 2, 2 for i, c in enumerate(text): x = x0 + i % w * xInter y = y0 + i // w * yInter node = svg.draw(draw_text_only(x, y, text=c)) svg.set_node(node, 'text-anchor', 'middle') svg.set_node(node, 'dominant-baseline', 'central') svg.set_node(node, 'fill', random_color()) str_tmp = 'rotate({},{},{})'.format(theta, x, y) svg.set_node(node, 'transform', str_tmp) # svg.draw(draw_circle(x, y, 5, color='red')) theta += 90
def drawPathContinuPoints(svg, pts, strokeWidth=0.5, color=None): path = 'M ' for i in range(len(pts)): x, y = pts[i] path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) svg.draw(draw_path(path, stroke_width=strokeWidth, color=color or random_color()))
def draw_circleRings(x, y, radius, rings=5, color=None, fillColor='white'): """Draw circles rings for svg""" for _ in range(rings): r = random.randint(1, rings) * radius / (rings + 1) sw = random.choice([1, 1, 1, 2, 2, 3]) color = color or random_color() yield f'<circle cx="{x}" cy="{y}" r="{r}" stroke-width="{sw}" \
def drawTrianglePoints(svg, pt1, pt2, pt3, stroke_width=0.1, color=None): pts = [] pts.append((pt1[0], pt1[1], pt2[0], pt2[1])) # pt1,pt2 pts.append((pt1[0], pt1[1], pt3[0], pt3[1])) # pt1,pt3 pts.append((pt2[0], pt2[1], pt3[0], pt3[1])) # pt2,pt3 drawlinePoints(svg, pts, stroke_width=stroke_width, color=color or random_color())
def image_svg_path2(file, dst_file): """convert raster image to svg by using path element """ image = get_binary_image(file) W = image.shape[1] H = image.shape[0] svg = SVGFileV2(dst_file, W=W, H=H, border=True) paths = get_potrace_path(image) N = 4 zoom = 1 / N for i in range(N): for j in range(N): to_point = (i * W / N, j * H / N) path = path_potrace_jagged_trans(paths, zoom_x=zoom, zoom_y=zoom, to_point=to_point) fill_color = random_color() svg.draw( draw_path(path, color='none', fill_color=fill_color, fill_rule='evenodd'))
def drawOneFuncSVG(svg, ptX, ptY, N=10, color=None): x = ptX[0] y = ptY[0] path = 'M %.1f %.1f L ' % (x, y) for x, y in zip(ptX, ptY): path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) svg.draw(draw_path(path, stroke_width=0.5, color=color or random_color()))
def drawPloygon(svg, pts, color=None, stroke_color=None): # print('pts',pts) points = [] for i in pts: points.append(str(clip_float(i[0])) + ',' + str(clip_float(i[1])) + ' ') points = ''.join(points) svg.draw(draw_polygon(points, stroke_width=0.5, color=color or random_color(), stroke_color=stroke_color))
def drawRandomCirclePath(svg): W, H = svg.get_size() styles = ['circles', 'circle points', 'circle points random'] onlyPath = False times = 100 r = 2 offsetX = W // 2 offsetY = H // 2 style = styles[1] if style == styles[0]: for _ in range(times): r = r + random.random() * 8 ptX, ptY = getCirclePtsSVG(svg, r=r, N=200, offsetX=offsetX, offsetY=offsetY, noise=False, onlyPath=onlyPath) drawOnePathcSVG(svg, ptX, ptY, onlyPath=onlyPath) elif style == styles[1]: times = 10 for _ in range(times): r = r + random.random() * 18 ptX, ptY = getCirclePtsSVG(svg, r=r, N=20, offsetX=offsetX, offsetY=offsetY, noise=False, onlyPath=onlyPath) ptNumber = int(5 * r) # ptX = np.random.choice(ptX, ptNumber) ptX = ptX.reshape((len(ptX), 1)) ptY = ptY.reshape((len(ptY), 1)) pts = np.concatenate((ptX, ptY), axis=1) # print(ptX.shape, pts.shape) ptsIndex = np.random.randint(len(pts), size=ptNumber) # print('ptsIndex=', ptsIndex, len(pts)) pts = pts[ptsIndex, :] for i in pts: # print('i=', i) # ra = 0.5 ra = np.random.random() * (3 - 0.2) + 0.2 ra = clip_float(ra) x = clip_float(i[0]) y = clip_float(i[1]) svg.draw(draw_circle(x, y, radius=ra, color=random_color()))
def createCircle(svg, x, y, r, color=None): id = 'circle_' + rand_str(4) color = color or random_color() circle = svg.draw(draw_circle(x, y, r, color=color)) svg.set_node(circle, 'id', id) if 1: # rings svg.set_node(circle, 'fill', 'none') svg.set_node(circle, 'stroke-width', "2") svg.set_node(circle, 'stroke', color) return id, circle
def drawOnePathcSVG(svg, ptX, ptY, width=1, onlyPath=True): x = ptX[0] y = ptY[0] path = 'M %.1f %.1f L ' % (x, y) for x, y in zip(ptX, ptY): path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) path = path + 'z' if onlyPath: svg.draw((path)) else: svg.draw(draw_path(path, stroke_width=width, color=random_color()))
def drawlinePointsContinus(svg, pts, stroke_width=0.5, color=None, stroke_widths=None): n = len(pts) for i in range(n - 1): (x1, y1), (x2, y2) = pts[i], pts[i + 1] x1 = clip_float(x1) y1 = clip_float(y1) x2 = clip_float(x2) y2 = clip_float(y2) if stroke_widths: stroke_width = stroke_widths[i] svg.draw(draw_line(x1, y1, x2, y2, stroke_width=stroke_width, color=color or random_color()))
def draw_basic_shapes(svg): svg.draw(sb.draw_rect(x=3, y=3, width=20, height=20, stroke_width=1, color='transparent', stroke_color='black')) rt = svg.draw(sb.draw_rect(x=28, y=3, width=20, height=20, stroke_width=1, color='transparent', stroke_color='red')) svg.set_node(rt, 'rx', '2') svg.set_node(rt, 'ry', '2') svg.draw(sb.draw_circle(x=60, y=13, radius=10, color='green')) svg.draw(sb.draw_ring(x=85, y=13, radius=8, stroke_color='red', stroke_width=3)) svg.draw(sb.draw_ellipse(cx=25, cy=35, rx=20, ry=8)) svg.draw(sb.draw_line(x=50, y=30, x2=90, y2=45, stroke_width=1, color='#23ff67')) points = '8 50 18 70 28 50 38 70 48 50 58 70 68 50 78 70 88 50 98 70' svg.draw(sb.draw_polyline(points, stroke_width=0.5, stroke_color=random_color())) points = '8 75 25 75 30 95 5 95' svg.draw(sb.draw_polygon(points, stroke_width=0.5, stroke_color=random_color())) path = 'M 35 90 Q 55 60 98 90' svg.draw(sb.draw_path(path, stroke_width=0.2, color=random_color()))
def drawPointsLineGraphic8(svg): # Neuron network def getNumberYs(H, N=3): offsetY = 190 / N # hInter = (H - 2 * offsetY) / (N - 1) if N == 1: return [H / 2] return np.linspace(offsetY, H - offsetY, N) W, H = svg.get_size() # cx, cy = W // 2, H // 2 layerNumbers = [8, 6, 6, 4] ptsLayers = [] inter = 52 x0 = 15 for i, N in enumerate(layerNumbers): # x = x0 + i*inter xs = np.zeros((N,)) + x0 + i * inter ys = getNumberYs(H, N) # print('xs=', len(xs), xs) # print('ys=', len(ys), ys) ptLayer = np.stack(([xs, ys])).T # print('ptLayer=',ptLayer) ptsLayers.append(ptLayer) # print('ptsLayers=',ptsLayers) for i, layPts in enumerate(ptsLayers): x = layPts[0][0] - 15 y = layPts[0][1] - 8 # print(x,y) svg.draw(draw_text(x, y, 'layer' + str(i))) drawPointsCircle(svg, layPts, r=3, color=random_color()) for i in range(len(ptsLayers) - 1): layerPtsPre = ptsLayers[i] layerPts = ptsLayers[i + 1] # print('layerPtsPre=',layerPtsPre) # print('layerPts=',layerPts) linePoints = [] for pre in layerPtsPre: for cur in layerPts: # print('pre,cur=', pre, cur) conect = (pre[0], pre[1], cur[0], cur[1]) linePoints.append(conect) drawlinePoints(svg, linePoints, color='black')
def drawRandomNumbersPath(svg): W, H = svg.get_size() times = 100 N = 500 xW = 5 for _ in range(times): path = 'M 0 0 L ' ptX = np.arange(N) + xW ptY = randomContinueNumbers(N=N) # print(ptX) # print(ptY) for x, y in zip(ptX, ptY): path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) svg.draw(draw_path(path, stroke_width=0.2, color=random_color()))
def drawPloygonNode(svg, pts, node=None, color=None): # print('pts',pts) points = [str(clip_float(i[0])) + ',' + str(clip_float(i[1])) + ' ' for i in pts] points = ''.join(points) svg.draw_node(node, draw_polygon(points, stroke_width=0.5, color=color or random_color()))
def drawlinePoints(svg, pts, node=None, stroke_width=0.5, color=None, stroke_widths=None, dash=None): for i, pt in enumerate(pts): x1, y1, x2, y2 = pt x1 = clip_float(x1) y1 = clip_float(y1) x2 = clip_float(x2) y2 = clip_float(y2) if stroke_widths: stroke_width = stroke_widths[i] svg.draw_node(node, draw_line(x1, y1, x2, y2, stroke_width=stroke_width, color=color or random_color(), stroke_dasharray=dash))