Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for conversion from python bytes #79

Merged
merged 5 commits into from
May 2, 2024

Conversation

wlyh514
Copy link
Contributor

@wlyh514 wlyh514 commented Apr 18, 2024

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.

@@ -15,3 +15,20 @@ def convert_image_to_svg_py(image_path: str,
path_precision: Optional[int] = None, # default: 8
) -> None:
...

def convert_py(
img_bytes: bytes,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this indentation looks too deep haha. may be you are aligning with above?

Comment on lines 48 to 49
img_bytes: Vec<u8>,
img_format: Option<&str>, // Format of the image. If not provided, the image format will be guessed based on its contents.
Copy link
Member

@tyt2y3 tyt2y3 Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are the possible image format? e.g. rgba8888 ?

can you give some examples?

also, I'd say name it convert_raw_image_to_svg

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are image file formats supported by the image crate, listed here

@tyt2y3
Copy link
Member

tyt2y3 commented Apr 19, 2024

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?

@wlyh514
Copy link
Contributor Author

wlyh514 commented Apr 19, 2024

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.

@tyt2y3 tyt2y3 force-pushed the master branch 2 times, most recently from 9f1c2c2 to 725adf5 Compare April 20, 2024 14:49
@wlyh514
Copy link
Contributor Author

wlyh514 commented Apr 23, 2024

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.

@tyt2y3
Copy link
Member

tyt2y3 commented Apr 26, 2024

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.

@wlyh514
Copy link
Contributor Author

wlyh514 commented Apr 26, 2024

Done, please check it out

@tyt2y3
Copy link
Member

tyt2y3 commented Apr 26, 2024

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?

@wlyh514
Copy link
Contributor Author

wlyh514 commented May 1, 2024

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

@tyt2y3
Copy link
Member

tyt2y3 commented May 1, 2024

Thank you, let me try this out.

@tyt2y3 tyt2y3 merged commit 1aff9a3 into visioncortex:master May 2, 2024
@tyt2y3
Copy link
Member

tyt2y3 commented May 2, 2024

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

@wlyh514
Copy link
Contributor Author

wlyh514 commented May 6, 2024

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

@tyt2y3
Copy link
Member

tyt2y3 commented May 7, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants