Last active
August 2, 2022 03:16
-
-
Save zgoda/16c4bb767a085743251503471c1faeb1 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 | |
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() |
Same issue with me. Changing it the RGBA
fixed it. THanks @kt-- I would have never solved this without your comment :)
Thanks guys. The ARGB
was working for couple my images so I assumed it's common setting.
I also had the same issue, maybe it's time to update the gist?
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.
Still seems to work fine for me, although it may have issues with using
SVG's made in illustrator
…On Fri, Dec 25, 2020 at 4:23 AM Jarek Zgoda ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
I also had the same issue, maybe it's time to update the gist?
OK, I updated the gist. Note that pynanosvg
<https://github.com/ethanhs/pynanosvg> has been archived and is no longer
maintained so please use with caution.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/16c4bb767a085743251503471c1faeb1#gistcomment-3572825>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJJNZKYV2ALUGXZGJ2KJYDLSWR74VANCNFSM4HL6SNPA>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Good Star RGBA
Star SVG: (I can't upload it, computer says "No")