Closed MestreLion closed 2 years ago
Hi @MestreLion, do you know if Pygame can read in images from Numpy arrays? If so, you should be able to use sdl2.ext.surface_to_ndarray
to convert it to a Numpy array, then load that into a Pygame surface (either directly or by casting the array to bytes).
No clue if it's possible without the additional Numpy dependency, but the source for sdl2.ext.surface_to_ndarray
might give you some ideas!
@a-hurst , thanks for the reply! Yes, there are ways to get pygame to create a surface out of a numpy array (either with pygame.image.frombuffer()
or pygame.surfarray
, tho the latter, surprisingly, does not take an RGBA one!
In any case, although I eventually managed to get pygame to read the output of sdl2.ext.surface_to_ndarray
, I have to give up on the whole SDL2 approach. It seems SVG features supported by SDL_image are still in its infant stages: it wasn't able to properly render any of the card themes I'm using, some of them were barely rendered at all. (performance is impressive tho!)
So I kind of had to go back to the old (and awkward) method of LibRsvg + Pycairo (+ Pillow). None of the other SVG renders I tried, such as cairosvg, svglib, etc, were able to render all elements in all card themes. (and their performance is abysmal anyway)
@MestreLion Ah yep, SDL_image
uses nanoSVG as a backend, which is small/simple/portable but is definitely a bit limited when it comes to full-fat SVG libraries. My own testing found that older and simpler SVGs were generally okay, but newer and more complex ones could break dramatically.
Glad you found something that works, though I hope something better for Python SVG rendering comes along!
This is actually a question, not a feature request, but I'm willing to create a PR for docs and/or new method if anyone finds this useful:
Currently I'm planning to use
py-sdl2
for the sole purpose of loading SVG files into Pygame, aspygame.image.load()
now does accept SVG files but it lackswidth
,height
arguments to choose the rendering size, so it renders only to nominal size (similar tosdl2.sdlimage.IMG_LoadSVG_RW
), which is kind of useless.That said, I believe
sdl2.ext.image.load_svg()
is the proper tool for the job. But... how can I convert the object returned to apygame.Surface
? I guess the two are very similar, so how to go from one to the other?I believe it would be something like:
Any help is greatly appreciated.
If needed, I can send a PR to add this "recipe" to one of pysdl2's tutorials or create an ext wrapper for it.