-
-
Save charlsagente/448372ba4cd98f8f80d9273b9937d78d to your computer and use it in GitHub Desktop.
Load svg into Pygame image using pynanosvg (https://github.com/ethanhs/pynanosvg)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from svg import Parser, Rasterizer | |
def load_svg(filename, scale=None, size=None, clip_from=None, fit_to=None): | |
"""Returns Pygame Image object from rasterized SVG | |
If scale (float) is provided and is not None, image will be scaled. | |
If size (w, h tuple) is provided, the image will be clipped to specified size. | |
If clip_from (x, y tuple) is provided, the image will be clipped from specified point. | |
If fit_to (w, h tuple) is provided, image will be scaled to fit in specified rect. | |
""" | |
svg = Parser.parse_file(filename) | |
tx, ty = 0, 0 | |
if size is None: | |
w, h = svg.width, svg.height | |
else: | |
w, h = size | |
if clip_from is not None: | |
tx, ty = clip_from | |
if fit_to is None: | |
if scale is None: | |
scale = 1 | |
else: | |
fit_w, fit_h = fit_to | |
scale_w = float(fit_w) / svg.width | |
scale_h = float(fit_h) / svg.height | |
scale = min([scale_h, scale_w]) | |
rast = Rasterizer() | |
req_w = int(w * scale) | |
req_h = int(h * scale) | |
buff = rast.rasterize(svg, req_w, req_h, scale, tx, ty) | |
image = pygame.image.frombuffer(buff, (req_w, req_h), 'ARGB') | |
return image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment