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

Get Image as bytes? #25

Open
ghmendonca opened this issue Dec 7, 2023 · 1 comment
Open

Get Image as bytes? #25

ghmendonca opened this issue Dec 7, 2023 · 1 comment

Comments

@ghmendonca
Copy link

ghmendonca commented Dec 7, 2023

Right now, looks like there is no way to get the image as an array of bytes (&[u8]). And since I'm not going to store the image into a file, but instead return the image from a HTTP request, I really need that. Would be nice to have something like image.to_bytes() or image.as_bytes().

Right now I'm getting the bytes by encoding to the same format the image already is, like this:

let mut bytes = Vec::new();
self.image.encode(self.format, &mut bytes).unwrap();

But of course this is allocating more memory that shouldn't be needed.

@simxnet
Copy link

simxnet commented Sep 12, 2024

Actually I found an approach for this, it's a bit hacky tho.

image.data.iter().map(|p| p.as_bytes().as_slice()).collect::<&[u8]>()

i did not test it hehe

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

No branches or pull requests

2 participants