Python Forum
SVG + Python experiments (using pygame-ce and/or cairoSVG)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SVG + Python experiments (using pygame-ce and/or cairoSVG)
#1
Video 
I've been making different experiments/demos with Nodezator and other Python libraries in order to identify programming tasks that have synergy with a node-based approach. This experiment shows what I've learned about Python and SVG images. What I learned is actually very useful regardless of whether you are using text-based or node-based Python.

Some people don't even know pygame/pygame-ce 2 has support for SVG. Although limited, SVG support in pygame can still be used to achieve a lot of advanced stuff. If you need more complete support, cairoSVG is a great alternative to render SVG graphics.

Because SVG is so versatile and is just really a text format, you can create complex smoothly antialiased shapes/paths with it by writing your own instructions. Like this, for instance, where SVG text is rendered as a still image (a pygame surface) with pygame-ce:

from io import BytesIO # standard lib

import pygame

svg_text = """
<svg width="300" height="200">
    <rect x="0" y="0" width="300" height="200" fill="white" />
    <circle cx="150" cy="100" r="60" fill="red" />
</svg>
""".strip()

svg_bytes = bytes(svg_text, encoding='utf-8')
bytestream = BytesIO(svg_bytes) # or "with BytesIO(svg_bytes) as bytestream: ..."
surface = pygame.image.load(bytestream)
Here are relevant resources to know more about this:

A ~10 min video showing my experiments:



Also a text post version (source code included).
Gribouillis likes this post
Reply


Messages In This Thread
SVG + Python experiments (using pygame-ce and/or cairoSVG) - by KennedyRichard - Oct-23-2023, 02:54 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020