Creating an SVG file with generativepy

Martin McBride, 2021-11-09
Tags generativepy tutorial svg
Categories generativepy generativepy tutorial

In this article we will see how to create an SVG file using generativepy.

SVG files

SVG (Standard Vector Graphics) is a file format that stores a vector image. Vector images can be printed at any size without blurring or loss of detail.

SVG files can also be opened by most vector drawing programs (for example, Inkscape) and edited. This allows you to use generativepy to create an image, and then add finishing touches manually with a drawing program.

Creating an SVG

Creating an SVG file is very easy. It is exactly the same as creating a PNG file, except that you call make_svg instead of `make_image:

from generativepy.drawing import setup, make_svg
from generativepy.color import Color
from generativepy.geometry import Rectangle

def draw(ctx, pixel_width, pixel_height, frame_no, frame_count):

    setup(ctx, pixel_width, pixel_height, background=Color(0.4))

    color = Color(1, 0.5, 0)

    Rectangle(ctx).of_corner_size((100, 150), 250, 200).fill(color)

make_svg("rectangle-svg.svg", draw, 500, 400)

One thing to note is that the page size is just a nominal value. You can easily resize or scale the image in Inkscape.

This code is available on github in tutorial/getting-started/

If you found this article useful, you might be interested in the book Computer Graphics in Python or other books by the same author.


Popular tags

2d arrays abstract data type alignment and angle animation arc array arrays bar chart bar style behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data science data types decorator design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop function function composition function plot functools game development generativepy tutorial generator geometry gif global variable gradient greyscale higher order function hsl html image image processing imagesurface immutable object in operator index inner function input installing iter iterable iterator itertools join l system lambda function len lerp line line plot line style linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map marker style matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations pie chart polygon positional parameter print pure function python standard library radial gradient range recipes rectangle recursion reduce regular polygon repeat rgb rotation roundrect scaling scatter plot scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip