def draw_heart_curve(): file = gImageOutputPath + r'\heartPath.svg' H, W = 100, 100 svg = SVGFileV2(file, W, H) offsetX = W // 2 offsetY = H // 2 svg.draw(add_style_path(stroke='red', stroke_width=0.5, fill='red')) N = 100 r = 30 path = 'M %.1f %.1f L ' % (0 + offsetX, heartFuc(0, r) + offsetY ) # start point x = np.linspace(-r, r, N) y = heartFuc(x, r=r) # Up part points of heart curve, set sqrt value positive xr = np.flip(x) # Down part points of heart curve, set sqrt value negative yr = heartFuc(xr, r=r, up=False) x = np.concatenate((x, xr), axis=0) y = np.concatenate( (y, yr), axis=0 ) * -1 # *-1 svg coordinate system different from standard cod system # print('x=', x) # print('y=', y) x = x + offsetX y = y + offsetY for i, j in zip(x, y): path = path + ' ' + str(clip_float(i)) + ' ' + str(clip_float(j)) svg.draw(draw_only_path(path)) svg.close()
def main(): file = gImageOutputPath + r'\test.svg' H, W = 100, 100 svg = SVGFileV2(file, W, H, border=True) # drawArtSvg() # draw_first(svg) draw_basic_shapes(svg)
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 Socrates(): file = gImageOutputPath + r'\Socrates.svg' svg = SVGFileV2(file, W=660, H=800, border=True) dataDict = {} dataDict['name'] = 'Socrates' dataDict['name_cn'] = '苏格拉底' dataDict['wiki'] = 'https://en.wikipedia.org/wiki/Socrates' dataDict['date_birth'] = '470 BC' dataDict['date_death'] = '399 BC' dataDict['photo'] = os.path.abspath(r'.\res\download\Socrates.jpg') dataDict['profile'] = 'Ancient Greek philosopher' dataDict['quotes'] = [] dataDict['quotes'].append('An unexamined life is not worth living.'.splitlines()) dataDict['quotes'].append('One thing only I know, and that is that I know nothing.'.splitlines()) dataDict['quotes'].append('True knowledge exists in knowing that you know nothing.'.splitlines()) dataDict['quotes'].append('I know that I am intelligent, because I know that I know nothing.'.splitlines()) dataDict['quotes'].append('I cannot teach anybody anything, I can only make them think.'.splitlines()) dataDict['quotes'].append('To find yourself, think for yourself.'.splitlines()) dataDict['quotes'].append('Education is the kindling of a flame, not the filling of a vessel.'.splitlines()) dataDict['quotes'].append('An honest man is always a child.'.splitlines()) dataDict['quotes'].append('The only true wisdom is in knowing you know nothing.'.splitlines()) dataDict['quotes'].append('There is only one good, knowledge, and one evil, ignorance.'.splitlines()) dataDict['quotes'].append('Wonder is the beginning of wisdom.'.splitlines()) dataDict['quotes'].append('He who is not contented with what he has, would not \rbe contented with what he would like to have.'.splitlines()) ProfileStyleSimple(svg, dataDict)
def Nietzsche(): file = gImageOutputPath + r'\Nietzsche.svg' svg = SVGFileV2(file, W=660, H=800, border=True) dataDict = {} dataDict['name'] = 'Friedrich Wilhelm Nietzsche' dataDict['name_cn'] = '弗里德里希·威廉·尼采' dataDict['wiki'] = 'https://en.wikipedia.org/wiki/Friedrich_Nietzsche' dataDict['date_birth'] = '15 October 1844' dataDict['date_death'] = '25 August 1900' dataDict['photo'] = os.path.abspath(r'.\res\download\nicai.jpg') dataDict['profile'] = 'German philosopher, cultural critic, composer, poet, writer.' dataDict['quotes'] = [] dataDict['quotes'].append('Without music, life would be a mistake.'.splitlines()) dataDict['quotes'].append('That which does not kill us makes us stronger.'.splitlines()) dataDict['quotes'].append('To live is to suffer, to survive is to find some \rmeaning in the suffering.'.splitlines()) dataDict['quotes'].append('We should consider every day lost on which we have not \rdanced at least once.'.splitlines()) dataDict['quotes'].append('A person who knows why he lives, can endure any kind of life.'.splitlines()) dataDict['quotes'].append('An unfortunate marriage is not a lack of love, but \ra lack of friendship.'.splitlines()) dataDict['quotes'].append('God is dead.'.splitlines()) dataDict['quotes'].append('My time has not come, and some people are born after death.'.splitlines()) dataDict['quotes'].append('You might as well take a bold risk in life because \ryou have to lose it.'.splitlines()) dataDict['quotes'].append('The man of knowledge must be able not only to \rlove his enemies but also to hate his friends.'.splitlines()) ProfileStyleSimple(svg, dataDict)
def main(): # drawText() # getSystemFonts() file = gImageOutputPath + r'\text.svg' svg = SVGFileV2(file, W=200, H=200, border=True) # draw_poet(svg) draw_poet2(svg)
def __init__(self, imageFile, dstSvgfile, step=1): self.image = loadImg(imageFile, cv2.IMREAD_COLOR) # cv2.IMREAD_GRAYSCALE self.height = self.image.shape[0] self.width = self.image.shape[1] self.step = step self.svgH = int((self.height // step) * step) self.svgW = int((self.width // step) * step) self.svg = SVGFileV2(dstSvgfile, W=self.svgW, H=self.svgH) print('step=', step, 'image H,W=', self.height, self.width, 'SVG H,W=', self.svgH, self.svgW)
def drawArtSvg(): styles = DrawArt().styles recurse = [4, 5] for N in recurse: for style in styles: fileName = 'art_' + style + '_' + str(N) + '.svg' file = os.path.join(gImageOutputPath, fileName) H, W = 200, 200 svg = SVGFileV2(file, W, H) draw = DrawArt(svg) draw.plotArt(np.array([0, 0]), np.array([W, H]), N=N, style=style)
def image_svg_path3(file, dst_file): """potrace to multi <path/> elements """ image = get_binary_image(file) svg = SVGFileV2(dst_file, W=image.shape[1], H=image.shape[0], border=True) paths = get_potrace_path(image) for i, path in enumerate(my_path_potrace(paths)): print(f'[{i}]', 'path=', path) svg.draw( draw_path(path, stroke_width=1.8, color='none', fill_color='#000000', fill_rule='evenodd'))
def drawPointLine(): file = gImageOutputPath + r'\pointsLine.svg' svg = SVGFileV2(file, W=200, H=200, border=True) # drawPointsLineGraphic(svg) # drawPointsLineGraphic2(svg) # drawPointsLineGraphic3(svg) # drawPointsLineGraphic4(svg) # drawPointsLineGraphic5(svg) # drawPointsLineGraphic6(svg) # drawPointsLineGraphic7(svg) # drawPointsLineGraphic8(svg) # drawPointsLineGraphic9(svg) # drawPointsLineGraphic10(svg) # drawPointsLineGraphic11(svg) # drawPointsLineGraphic12(svg) drawPointsLineGraphic13(svg)
def main(): file = gImageOutputPath + r'\animation.svg' svg = SVGFileV2(file, W=200, H=200, border=True) # animCircleInflation(svg) # animCircleInflation2(svg) # animCircleInflation3(svg) # animCircleInflation4(svg) # animCircleInflation5(svg) # anim_Windmill(svg) # drawAny(svg) # anim6(svg) # anim7(svg) # anim8(svg) # anim9(svg) anim10(svg)
def testSmile2(): d = gImageOutputPath + r'\smileC2.svg' N = 6 inter = 5 offsetX = 0 offsetY = 0 r0 = 10 totalW = N * (N - 1) * r0 + N * inter + offsetX totalH = (N - 1) * 2 * r0 + offsetY svg = SVGFileV2(d, W=totalW, H=totalH) # svg = SVGSmile(dstSvgfile=d, svgW=totalW, svgH=totalH) for i in range(1, N): r = i * r0 drawSmileSVG(svg, ridus=r, offsetX=offsetX, offsetY=offsetY) offsetX += (2 * r + inter)
def image_svg_path(file, dst_file): """convert raster image to svg by using path element """ image = get_binary_image(file) svg = SVGFileV2(dst_file, W=image.shape[1], H=image.shape[0], border=True) paths = get_potrace_path(image) # path = path_potrace(paths) path = path_potrace_jagged(paths) print('len(path)=', len(path)) fill_color = random_color_hsv() # 'black' svg.draw( draw_path(path, stroke_width=1.8, color='none', fill_color=fill_color, fill_rule='evenodd'))
def drawLineGraphic(): file = gImageOutputPath + r'\lineGraphic.svg' svg = SVGFileV2(file, W=200, H=200, border=True) # drawLineGrapic(svg) # drawLineGrapic2(svg) # drawLsoscelesTrianglePoints(svg) # drawRandomTrianglePoints(svg) # drawRandomTriangles(svg) # drawAbstractLine(svg) # drawArrowCircleLine(svg) # drawLineGrapic3(svg) # drawLineGrapic4(svg) # drawLineGrapic5(svg) # drawLineGrapic6(svg) # drawLineGrapic7(svg) # drawLineGrapic8(svg) drawLineGrapic9(svg)
def drawText(): def GeFile(): file = r'.\res\hi.txt' with open(file, 'r', encoding='utf-8') as f: return f.readlines() file = gImageOutputPath + r'\Hi.svg' H, W = 200, 1200 # str='Hello' svg = SVGFileV2(file, W, H, border=False) styleDict = {} styleDict['fill'] = 'black' styleDict['font-family'] = 'Consolas' styleDict['font-size'] = '10px' styleDict['font-style'] = 'normal' styleDict['font-variant'] = 'normal' # styleDict['xml:space'] = 'preserve' #deprecated styleDict['white-space'] = 'pre' styleList = get_styles(styleDict) svg.draw(add_style('text', styleList)) if 0: y0 = 15 for i in range(10): svg.draw(draw_text(0, y0, '. Hello World! 1')) y0 += 12 else: strs = GeFile() y0 = 15 h = 12 for i in strs: # i = i.replace('#', '@') i = ' '.join(i) # '.'.join(i) print(i) # svg.draw(draw_text(0,y0,i, blank_space='preserve')) # text = svg.draw(draw_text_only(0,y0,i)) # attr = "{{{}}}".format(svg.namespace) + 'space' # # svg.set_node(text, attr,"preserve") #error without namespace y0 += h svg.close()
def imgSvgElement(): file = gImageOutputPath + r'\image.svg' svg = SVGFileV2(file, W=200, H=200, border=False) W, H = svg.get_size() styleDict = {} styleDict['x'] = '0' styleDict['y'] = '0' # 'green' styleDict['width'] = 100 styleDict['height'] = 100 # styleDict['href'] = 'https://www.python.org/static/img/[email protected]' styleDict['href'] = r'../images/download.png' # styleDict['preserveAspectRatio'] = 'none' # styleDict['crossorigin'] = '' svg.draw(draw_any('image ', **styleDict)) styleDict['x'] = '20' styleDict['y'] = '20' # 'green' svg.draw(draw_any('image ', **styleDict))
def drawRandomPath(): file = gImageOutputPath + r'\randomShapePath.svg' H, W = 500, 1000 svg = SVGFileV2(file, W, H) singleColor = False if singleColor: onlyPath = True color = '#33FFC9' svg.draw( add_style_path(stroke=color, stroke_width=1, fill='transparent')) else: onlyPath = False times = 200 r = 1 offsetX = 50 # W//2 # offsetY = H // 2 for _ in range(times): r = r + random.random() * 2 # r = r + random.normalvariate(mu=0,sigma=1)*8 offsetX = offsetX + random.random() * 5 # 8 # offsetX = offsetX + random.normalvariate(mu=0,sigma=1)*1 # offsetY = offsetY + random.random()*1 # offsetX = 50 + random.random()*10 # offsetY = 50 + random.random()*2 ptX, ptY = getCirclePtsSVG(svg, r=r, N=80, offsetX=offsetX, offsetY=offsetY, noise=True, onlyPath=onlyPath) drawOnePathcSVG(svg, ptX, ptY, onlyPath=onlyPath)
def testSmile3(): d = gImageOutputPath + r'\smileC3.svg' H, W = 6, 6 inter = 5 offsetX = 0 offsetY = 0 r0 = 20 totalW = W * 2 * r0 + W * inter + offsetX totalH = H * 2 * r0 + H * inter + offsetY svg = SVGFileV2(d, W=totalW, H=totalH) for i in range(H): for j in range(W): r = r0 offsetX = j * (2 * r + inter) offsetY = i * (2 * r + inter) # drawSmileSVG(svg,radius = r,offsetX=offsetX,offsetY=offsetY,color=random_color()) drawSmileSVG(svg, radius=r, offsetX=offsetX, offsetY=offsetY, color='#FFC10E')
def KarlPopper(): file = gImageOutputPath + r'\KarlPopper.svg' svg = SVGFileV2(file, W=660, H=800, border=True) dataDict = {} dataDict['name'] = 'Karl Raimund Popper' dataDict['name_cn'] = '卡尔·雷蒙德·波普尔' dataDict['wiki'] = 'https://en.wikipedia.org/wiki/Karl_Popper' dataDict['date_birth'] = '28 July 1902' dataDict['date_death'] = '17 September 1994' dataDict['photo'] = os.path.abspath(r'.\res\download\Karl_Popper.jpg') dataDict['profile'] = ' Austrian-British philosopher, \rand social commentator' dataDict['quotes'] = [] dataDict['quotes'].append('Science must begin with myths, and with the criticism of myths.'.splitlines()) dataDict['quotes'].append('Unlimited tolerance must lead to the disappearance of tolerance.'.splitlines()) dataDict['quotes'].append('Our knowledge can only be finite, while our ignorance must \rnecessarily be infinite.'.splitlines()) dataDict['quotes'].append('True ignorance is not the absence of knowledge, \rbut the refusal to acquire it.'.splitlines()) dataDict['quotes'].append('Those who promise us paradise on earth never \rproduced anything but a hell.'.splitlines()) dataDict['quotes'].append('A theory that explains everything, explains nothing.'.splitlines()) dataDict['quotes'].append('All life is problem solving.'.splitlines()) dataDict['quotes'].append('While differing widely in the various little bits we know, \rin our infinite ignorance we are all equal.'.splitlines()) dataDict['quotes'].append('Science may be described as the art of systematic \roversimplification.'.splitlines()) ProfileStyleSimple(svg, dataDict)
def main(): file = gImageOutputPath + r'\dataFrame.svg' svg = SVGFileV2(file, W=200, H=120, border=True) drawDataFrame(svg)
def testSmile(): file = gImageOutputPath + r'\smileC.svg' s = SVGFileV2(file, W=300, H=300) drawSmileSVG(s, radius=100, offsetX=20, offsetY=45)
svg.draw_node(g, draw_any('path', **anyDict)) anyDict['d'] = 'M 70 50 C 70 80, 110 80, 110 50' svg.draw_node(g, draw_any('path', **anyDict)) anyDict['d'] = 'M 130 50 C 120 80, 180 80, 170 50' svg.draw_node(g, draw_any('path', **anyDict)) # anyDict['d'] = 'M 10 10 10 60 60 30' # svg.draw_node(g, draw_any('path', **anyDict)) anyDict['d'] = 'M 10 315 \ L 110 215 \ A 30 50 0 0 1 162.55 162.45 \ L 172.55 152.45 \ A 30 50 -45 0 1 215.1 109.9 \ L 315 10' anyDict['fill'] = 'green' anyDict['stroke-width'] = '2' svg.draw_node(g, draw_any('path', **anyDict)) if __name__ == '__main__': # drawRandomPath() # draw_heart_curve() file = gImageOutputPath + r'\randomShapePath.svg' svg = SVGFileV2(file, W=200, H=200, border=True) drawRandomCirclePath(svg) # drawRandomRectanglePath(svg) # drawAllTypePath(svg)
# 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 drawRandomWalkPath(svg): W, H = svg.get_size() times = 1000 cx, cy = W // 2, H // 2 x = cx y = cy path = 'M %.1f %.1f L ' % (x, y) for _ in range(times): x, y = random_walk(x, y, 1, step=2) path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) svg.draw(draw_path(path, stroke_width=0.2)) if __name__ == '__main__': file = gImageOutputPath + r'\randomWalkPath.svg' H, W = 100, 100 svg = SVGFileV2(file, W, H) # drawRandomNumbersPath(svg) drawRandomWalkPath(svg)
def main(): file = gImageOutputPath + r'\func.svg' svg = SVGFileV2(file, 100, 100, border=True) # drawFuncSVG(svg, offsetX=10, offsetY=10) drawFuncSVG2(svg)
def main(): file = os.path.join(gImageOutputPath, 'svgPath.svg') svg = SVGFileV2(file, W=200, H=200, border=True) svg_path_basic(svg)
def main(): data = readSound(N=-1) file = gImageOutputPath + r'\soundGraphic.svg' svg = SVGFileV2(file, W=500, H=200, border=True) drawSoundGrapic(svg, data)