Creating an SVG file with generativepy
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/rectangle-svg.py.
See also
Join the PythonInformer Newsletter
Sign up using this form to receive an email when new content is added:
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 formula 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 latex 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 pil pillow polygon pong positional parameter print product programming paradigms programming techniques 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 tex text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip zip_longest