{"id":2995,"date":"2010-04-07T17:40:05","date_gmt":"2010-04-07T09:40:05","guid":{"rendered":"http:\/\/nick.onetwenty.org\/?p=2995"},"modified":"2011-03-28T22:46:00","modified_gmt":"2011-03-28T14:46:00","slug":"creating-svg-files-with-python","status":"publish","type":"post","link":"https:\/\/nick.onetwenty.org\/index.php\/2010\/04\/07\/creating-svg-files-with-python\/","title":{"rendered":"Creating SVG files with Python"},"content":{"rendered":"<p>It&#8217;s easy to create <a href=\"http:\/\/www.w3.org\/Graphics\/SVG\/\">SVG<\/a> files using <a href=\"http:\/\/www.python.org\/\">Python<\/a>. There&#8217;s no need for <a href=\"http:\/\/www.cairographics.org\/pycairo\/\">fancy rendering libraries<\/a>. Just use a nice XML import\/export library such as <a href=\"http:\/\/docs.python.org\/library\/xml.etree.elementtree.html\">ElementTree<\/a>.<\/p>\n<p>This code:<\/p>\n<pre lang=\"python\" line=\"1\">\r\n# my favourite XML library\r\nfrom xml.etree import ElementTree as et\r\n\r\n# create an SVG XML element (see the SVG specification for attribute details)\r\ndoc = et.Element('svg', width='480', height='360', version='1.1', xmlns='http:\/\/www.w3.org\/2000\/svg')\r\n\r\n# add a circle (using the SubElement function)\r\net.SubElement(doc, 'circle', cx='240', cy='180', r='160', fill='rgb(255, 192, 192)')\r\n\r\n# add text (using append function)\r\ntext = et.Element('text', x='240', y='180', fill='white', style='font-family:Sans;font-size:48px;text-anchor:middle;dominant-baseline:top')\r\ntext.text = 'pink circle'\r\ndoc.append(text)\r\n\r\n# ElementTree 1.2 doesn't write the SVG file header errata, so do that manually\r\nf = open('sample.svg', 'w')\r\nf.write('<?xml version=\\\"1.0\\\" standalone=\\\"no\\\"?>\\n')\r\nf.write('<!DOCTYPE svg PUBLIC \\\"-\/\/W3C\/\/DTD SVG 1.1\/\/EN\\\"\\n')\r\nf.write('\\\"http:\/\/www.w3.org\/Graphics\/SVG\/1.1\/DTD\/svg11.dtd\\\">\\n')\r\nf.write(et.tostring(doc))\r\nf.close()\r\n<\/pre>\n<p>Generates this image (.png file rendered from the output .svg):<\/p>\n<p><a href=\"https:\/\/i1.wp.com\/nick.onetwenty.org\/wp-content\/uploads\/2010\/04\/sample.png\"><img data-attachment-id=\"2997\" data-permalink=\"https:\/\/nick.onetwenty.org\/index.php\/2010\/04\/07\/creating-svg-files-with-python\/sample\/\" data-orig-file=\"https:\/\/i1.wp.com\/nick.onetwenty.org\/wp-content\/uploads\/2010\/04\/sample.png?fit=480%2C360&amp;ssl=1\" data-orig-size=\"480,360\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"sample\" data-image-description=\"\" data-medium-file=\"https:\/\/i1.wp.com\/nick.onetwenty.org\/wp-content\/uploads\/2010\/04\/sample.png?fit=300%2C225&amp;ssl=1\" data-large-file=\"https:\/\/i1.wp.com\/nick.onetwenty.org\/wp-content\/uploads\/2010\/04\/sample.png?fit=480%2C360&amp;ssl=1\" src=\"https:\/\/i1.wp.com\/nick.onetwenty.org\/wp-content\/uploads\/2010\/04\/sample.png?resize=480%2C360\" alt=\"\" title=\"sample\" width=\"480\" height=\"360\" class=\"aligncenter size-full wp-image-2997\" srcset=\"https:\/\/i1.wp.com\/nick.onetwenty.org\/wp-content\/uploads\/2010\/04\/sample.png?w=480&amp;ssl=1 480w, https:\/\/i1.wp.com\/nick.onetwenty.org\/wp-content\/uploads\/2010\/04\/sample.png?w=300&amp;ssl=1 300w\" sizes=\"(max-width: 480px) 85vw, 480px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p><em><strong>Note<\/strong>: I&#8217;m using something like this to generate nice diagrams for the <a href=\"http:\/\/nick.onetwenty.org\/index.php\/2010\/04\/06\/omglmg-lets-make-games-report\/\">next Let&#8217;s Make Games report<\/a>, and figured that it may be of use to others.<\/em><\/p>\n<p><em><strong>Update<\/strong>: Fixed typo in code (missing parentheses).<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s easy to create SVG files using Python. There&#8217;s no need for fancy rendering libraries. Just use a nice XML import\/export library such as ElementTree. This code: # my favourite XML library from xml.etree import ElementTree as et # create an SVG XML element (see the SVG specification for attribute details) doc = et.Element(&#8216;svg&#8217;, width=&#8217;480&#8242;, &hellip; <a href=\"https:\/\/nick.onetwenty.org\/index.php\/2010\/04\/07\/creating-svg-files-with-python\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Creating SVG files with Python&#8221;<\/span><\/a><\/p>\n","protected":false},"author":67,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paLsRH-Mj","_links":{"self":[{"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/posts\/2995"}],"collection":[{"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/users\/67"}],"replies":[{"embeddable":true,"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/comments?post=2995"}],"version-history":[{"count":9,"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/posts\/2995\/revisions"}],"predecessor-version":[{"id":5190,"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/posts\/2995\/revisions\/5190"}],"wp:attachment":[{"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/media?parent=2995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/categories?post=2995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nick.onetwenty.org\/index.php\/wp-json\/wp\/v2\/tags?post=2995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}