vtracer icon indicating copy to clipboard operation
vtracer copied to clipboard

Add support for conversion from python bytes

Open wlyh514 opened this issue 10 months ago • 6 comments

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.

wlyh514 avatar Apr 18 '24 19:04 wlyh514

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?

tyt2y3 avatar Apr 19 '24 08:04 tyt2y3

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.

wlyh514 avatar Apr 19 '24 10:04 wlyh514

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.

wlyh514 avatar Apr 23 '24 04:04 wlyh514

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.

tyt2y3 avatar Apr 26 '24 11:04 tyt2y3

Done, please check it out

wlyh514 avatar Apr 26 '24 17:04 wlyh514

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?

tyt2y3 avatar Apr 26 '24 22:04 tyt2y3

Yes that is correct, I have added use cases for the two functions in cmdapp/vtracer/README.md.

wlyh514 avatar May 01 '24 16:05 wlyh514

Thank you, let me try this out.

tyt2y3 avatar May 01 '24 17:05 tyt2y3

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

tyt2y3 avatar May 02 '24 21:05 tyt2y3

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 :)

wlyh514 avatar May 06 '24 14:05 wlyh514

thanks for sharing. yeah love it, game dev is always fun. I didnt get to do it ever since I graduated

tyt2y3 avatar May 07 '24 14:05 tyt2y3