vtracer
vtracer copied to clipboard
Add support for conversion from python bytes
This PR changes only the python binding of the library.
A new python function convert_py
is exposed to allow conversion from image bytes into a SVG string, without needing to interact with the file system. Doing so addresses discussion #73 .
This is not a breaking change, it did not change behaviour of already exposed functions.
There are no dependency changes involved.
Then may be I misunderstood your intention. I thought you're trying with raw image pixels. But what's the use for being able to pass in raw, undecoded image as bytes?
It allows python users to use the library without interacting with the file system. For instance, I was using this library on a backend server that receives raster images from an external source and serve the SVG string in the response body. Before this change I have to store the raster image in the fs, call convert_image_to_svg_py
, then read the svg from fs. With this change neither the read nor the write need to happen, the whole process can now be done within memory.
There's this discussion that called for this feature, so I thought it would be helpful to implement it.
Sorry I think I misunderstood your question. The primary reason for passing in raw bytes instead of pixels is to provide a more streightforward interface for the Python users, as in they don't have to do image loading and mode conversion on their side. i.e. they can do:
from vtracer import convert_raw_image_to_svg
svg = convert_raw_image_to_svg(img_bytes, 'png')
Instead of:
from vtracer import convert_raw_image_to_svg
from PIL import Image
img = Image(io.BytesIO(img_bytes))
img = img.convert('RGBA')
svg = convert_raw_image_to_svg(list(img.getdata()))
As we already have an image processing library on the Rust side, it feels unnecessary to require PIL for Python users imho. I could be wrong and please let me know if you want the function to accept pixels instead.
I'd say your convert_raw_image_to_svg
is useful, but ideally we'd also have convert_pixels_to_svg
that accepts RGBA pixel format.
Done, please check it out
Nice, can you also update the readme?
I assume this the correct usage right?
from vtracer import convert_pixels_to_svg
from PIL import Image
img = Image(io.BytesIO(img_bytes))
img = img.convert('RGBA')
svg = convert_pixels_to_svg(list(img.getdata()))
And convert_raw_image_to_svg
?
Yes that is correct, I have added use cases for the two functions in cmdapp/vtracer/README.md
.
Thank you, let me try this out.
Released! https://pypi.org/project/vtracer/0.6.11/
hey @wlyh514 I wonder what's your use case with VTracer? if it's something interesting could you share a bit
I was building a game where some game items are dynamically generated, I employed vtracer in the (item name) => (item game icon svg) pipeline. It worked amazingly well and I'm really impressed! Thank you for building this library :)
thanks for sharing. yeah love it, game dev is always fun. I didnt get to do it ever since I graduated