def effect(self): # メインのルート要素であるSVGを取得 svg = self.document.getroot() inkex.errormsg(str(svg)) image=svg.find(ALL+XMLNS+"image") width=inkex.unittouu(image.attrib["width"]) height=inkex.unittouu(image.attrib["height"]) #width = inkex.unittouu(svg.get('width')) #height = inkex.unittouu(svg.attrib['height']) #初期化の点 points=[[0,0],[width,0],[width,height],[0,height]] inkex.errormsg(u"画像属性" + str(image.attrib)) url=image.attrib[XLINK+"href"] inkex.errormsg((u"画像" + str(url))) #file.//の部分を取り除く im = Image.open(url[7:]) color=im.getpixel((0,0)) inkex.errormsg((u"色:" + str(color))) path=svg.find(ALL+XMLNS+"path") if path == None: inkex.errormsg(u"パスを書いてください!!") #パスの頂点座標を取得 vals=simplepath.parsePath(path.get('d')) for cmd,vert in vals: #たまに空のが入ってるため、それ対策 if len(vert) != 0: inkex.errormsg((u"頂点:" + str(vert))) points.append(vert) trignales,nodes = triangulate(points) inkex.errormsg(u"三角形分割の結果"+str(trignales)) i=0 for t in trignales: onedarray=t.parseSVG() attributes={"points":str(onedarray), "style":"fill:"+simplestyle.formatColor3i(color[0],color[1],color[2])+";stroke:white;stroke-width:3", "fill-opacity":"0.5"} polygon =inkex.etree.Element("polygon",attributes) svg.append(polygon) #for n in nodes: # circle=n.circle # inkex.errormsg(u"円:"+str(circle)) # attributes={"cx":str(circle.center.x),"cy":str(circle.center.y),"r":str(circle.radius), # "stroke":"yellow","stroke-width":"3","fill-opacity":"0.0"} # circle =inkex.etree.Element("circle",attributes) # svg.append(circle) inkex.errormsg(str(polygon))
def test_simplestyle(self): """Test simplestyle API""" import simplestyle self.assertEqual(simplestyle.svgcolors['blue'], '#0000ff') self.assertEqual(simplestyle.parseStyle('foo: bar; abc-def: 123em'), { 'foo': 'bar', 'abc-def': '123em' }) self.assertEqual(simplestyle.formatStyle({'foo': 'bar'}), 'foo:bar') self.assertTrue(simplestyle.isColor('#ff0000')) self.assertTrue(simplestyle.isColor('#f00')) self.assertTrue(simplestyle.isColor('blue')) self.assertFalse(simplestyle.isColor('none')) self.assertFalse(simplestyle.isColor('nosuchcolor')) self.assertEqual(simplestyle.parseColor('#0000ff'), (0, 0, 0xff)) self.assertEqual(simplestyle.parseColor('red'), (0xff, 0, 0)) self.assertEqual(simplestyle.formatColoria([0, 0x99, 0]), '#009900') self.assertEqual(simplestyle.formatColor3i(0, 0x99, 0), '#009900') self.assertEqual(simplestyle.formatColorfa([0, 1.0, 0]), '#00ff00') self.assertEqual(simplestyle.formatColor3f(0, 1.0, 0), '#00ff00')
def effect(self): #{{{ Check that elements have been selected if len(self.options.ids) == 0: inkex.errormsg(_("Please select objects!")) return #}}} #{{{ Drawing styles linestyle = { 'stroke': '#000000', 'stroke-width': str(self.unittouu('1px')), 'fill': 'none' } facestyle = { 'stroke': '#000000', 'stroke-width':'0px',# str(self.unittouu('1px')), 'fill': 'none' } #}}} #{{{ Handle the transformation of the current group parentGroup = self.getParentNode(self.selected[self.options.ids[0]]) svg = self.document.getroot() children =svg.getchildren() fp=open("log.txt","w") img=None width_in_svg=1 height_in_svg=1 for child in children: if child.tag=="{http://www.w3.org/2000/svg}g": ccc=child.getchildren() for c in ccc: if c.tag=="{http://www.w3.org/2000/svg}image": href=c.attrib["{http://www.w3.org/1999/xlink}href"] fp.write(href) img = Image.open(href) width_in_svg=child.attrib['width'] height_in_svg=child.attrib['height'] elif child.tag=="{http://www.w3.org/2000/svg}image": href=child.attrib["{http://www.w3.org/1999/xlink}href"] width_in_svg=child.attrib['width'] height_in_svg=child.attrib['height'] if "file://" in href: href=href[7:] fp.write(href+"\n") img = Image.open(href).convert("RGB") width=-1 height=-1 if img!=None: imagesize = img.size width=img.size[0] height=img.size[1] fp.write("imageSize="+str(imagesize)) trans = self.getGlobalTransform(parentGroup) invtrans = None if trans: invtrans = self.invertTransform(trans) #}}} #{{{ Recovery of the selected objects pts = [] nodes = [] seeds = [] fp.write('num:'+str(len(self.options.ids))+'\n') for id in self.options.ids: node = self.selected[id] nodes.append(node) if(node.tag=="{http://www.w3.org/2000/svg}path"):#pathだった場合 #パスの頂点座標を取得 points = cubicsuperpath.parsePath(node.get('d')) fp.write(str(points)+"\n") for p in points[0]: pt=[p[1][0],p[1][1]] if trans: simpletransform.applyTransformToPoint(trans, pt) pts.append(Point(pt[0], pt[1])) seeds.append(Point(p[1][0], p[1][1])) else:#その他の図形の場合 bbox = simpletransform.computeBBox([node]) if bbox: cx = 0.5 * (bbox[0] + bbox[1]) cy = 0.5 * (bbox[2] + bbox[3]) pt = [cx, cy] if trans: simpletransform.applyTransformToPoint(trans, pt) pts.append(Point(pt[0], pt[1])) seeds.append(Point(cx, cy)) pts.sort() seeds.sort() fp.write("*******sorted!***********"+str(len(seeds))+"\n") #}}} #{{{ Creation of groups to store the result # Delaunay groupDelaunay = inkex.etree.SubElement(parentGroup, inkex.addNS('g', 'svg')) groupDelaunay.set(inkex.addNS('label', 'inkscape'), 'Delaunay') #}}} scale_x=float(width_in_svg)/float(width) scale_y=float(height_in_svg)/float(height) fp.write('width='+str(width)+', height='+str(height)+'\n') fp.write('scale_x='+str(scale_x)+', scale_y='+str(scale_y)+'\n') #{{{ Voronoi diagram generation triangles = voronoi.computeDelaunayTriangulation(seeds) for triangle in triangles: p1 = seeds[triangle[0]] p2 = seeds[triangle[1]] p3 = seeds[triangle[2]] cmds = [['M', [p1.x, p1.y]], ['L', [p2.x, p2.y]], ['L', [p3.x, p3.y]], ['Z', []]] path = inkex.etree.Element(inkex.addNS('path', 'svg')) path.set('d', simplepath.formatPath(cmds)) middleX=(p1.x+p2.x+p3.x)/3.0/scale_x middleY=(p1.y+p2.y+p3.y)/3.0/scale_y fp.write("x:"+str(middleX)+" y:"+str(middleY)+"\n") if img!=None and imagesize[0]>middleX and imagesize[1]>middleY and middleX>=0 and middleY>=0: r,g,b = img.getpixel((middleX,middleY)) facestyle["fill"]=simplestyle.formatColor3i(r,g,b) else: facestyle["fill"]="black" path.set('style', simplestyle.formatStyle(facestyle)) groupDelaunay.append(path) fp.close()