Scripting Inkscape in Python by @schoolcoders

There are a million different ways to draw on a computer screen, whether it is Python, Java, Windows or HTML5. If you can do one you probably can have a decent stab at any of them. True, some of the old ones were quite primitive, but most modern canvas implementations have similar capabilities. They can draw basic shapes like rectangles and ellipses, make paths out of lines, arcs and curves. They can stroke paths with lines or dashes, fill shapes with colours, gradients and images, and handle transparency. The have built-in scaling and rotation, decent text handling, and most even support clipping.

This is a re-blog post originally posted by Martin McBride and published with kind permission.

The original post can be found here.

Do you have a blog post which you are proud of? Submit your blog post for reblogging on by clicking here.

inkscapeI’m using Inkscape quite a bit these days, to create diagrams for the website. Inkscape, of course, uses the SVG (Standard Vector Graphics) format. This is an XML based format for vector images. A vector format has the advantage that it is very good quality, and you can zoom and scale as much as you like without losing quality.

It has another advantage, which isn’t so obvious. Inkscape can edit any SVG file, not just one it has created itself. So if you create an SVG image in Python, you can still edit it in Inkscape, tight down to the element level. You created a Spirograph but you want the lines to be thinner and a different colour? No problem. You made a graph but you want the axis labels to be in a different font? Select then and edit.

You can even take your program generated SVG and paste it as an element in a bigger file, for example to add a graph to a poster. And when you are done you can export as PNG image of any resolution, or a variety of other vector formats (such as PDF).

So how do you do it in Python? Well I decided to use an open source module called svgwrite. And the good news is that most of its basic commands are very similar to any other drawing library you might be familiar with.

It is well documented, but the documentation assumes a bit of knowledge about how SVG itself works (which is fair enough, it is an SVG library after all). To make it a bit more accessible to students I have added a few pages on – see the Inkscape scripting section. There is an introduction and primer, plus some projects around creating graphs and Sprigraphs.

About UKEdChat Editorial 2600 Articles
The Editorial Account of UKEdChat. “Mastery is an unattainable illusion”

Be the first to comment

Leave a Reply

Your email address will not be published.