Skip to content

Instantly share code, notes, and snippets.

@zgoda
Last active August 2, 2022 03:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zgoda/16c4bb767a085743251503471c1faeb1 to your computer and use it in GitHub Desktop.
Save zgoda/16c4bb767a085743251503471c1faeb1 to your computer and use it in GitHub Desktop.
Load svg into Pygame image using pynanosvg (https://github.com/ethanhs/pynanosvg)
from svg import Parser, Rasterizer
import pygame
import sys
def load_svg(filename, mode='RGBA', 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), mode)
return image
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((480, 420))
img = load_svg('img.svg')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(pygame.colordict.THECOLORS['white'])
screen.blit(img, img.get_rect())
pygame.display.flip()
@kt--
Copy link

kt-- commented May 9, 2019

This was not working for me - with a plain object (opaque yellow star, transparent background, no border) on a 50% grey background -

I get a very-translucent yelowish star with a cyan-coloured-ish translucent border.

Changing the pygame.image.frombuffer() 'ARGB' to 'RGBA' fixes this for me. Using Pygame 1.9.4 on Python 3.6.7 on Linux.

Bad Star ARGB bad_star
Good Star RGBA good_star

Star SVG: (I can't upload it, computer says "No")

@metaMMA
Copy link

metaMMA commented Mar 1, 2020

Same issue with me. Changing it the RGBA fixed it. THanks @kt-- I would have never solved this without your comment :)

@zgoda
Copy link
Author

zgoda commented Mar 1, 2020

Thanks guys. The ARGB was working for couple my images so I assumed it's common setting.

@diVineProportion
Copy link

I also had the same issue, maybe it's time to update the gist?

@zgoda
Copy link
Author

zgoda commented Dec 25, 2020

I also had the same issue, maybe it's time to update the gist?

OK, I updated the gist. Note that pynanosvg has been archived and is no longer maintained so please use with caution.

@diVineProportion
Copy link

diVineProportion commented Dec 27, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment