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

Please amend documentation to show how to use rust image crate. #84

Open
zaddok opened this issue Sep 4, 2024 · 0 comments
Open

Please amend documentation to show how to use rust image crate. #84

zaddok opened this issue Sep 4, 2024 · 0 comments

Comments

@zaddok
Copy link

zaddok commented Sep 4, 2024

I am excited that this library exists, thanks for your work on this!

My system uses DynamicImage for the image processing, but this crate uses an alternate format for images it seems. It would be great if we can update the documentation to show how to bridge this gap. I have spent time trying to work out the best way to do it but my limited understanding of the underlying image data formats makes it difficult.

I've spent some time trying to work out how to convert from the image DynamicImage type into avif, and it is not 100% clear to me how it would be done. I have tried a few different ways, the following compiles, but returns img convert failed. How do we get the data out of a DynamicImage so we can pass it to this library?

Assuming this is how an image was opened, or created:

    let file = PathBuf::from(src);
    let ext = file.extension();
    if ext.is_none() {
        return Err(format!("source file must have file extension"));
    }
    let mut img = image::open(src).unwrap();

This is what I am trying to do, which doesn't work:

            use rgb::ComponentSlice;
            use rgb::FromSlice;
            use std::io::Write;
            let pr = match img.as_rgba8() {
                None => {
                    // Fails here
                    return Err(format!("img convert failed"));
                }
                Some(pr) => pr,
            };
            let pixels_rgba = pr.as_rgba();
            let (width, height) = img.dimensions();
            let ist = Img::new(pixels_rgba, width as usize, height as usize);
            match Encoder::new()
                .with_quality(75.)
                .with_speed(1)
                .encode_rgba(ist)
            {
                Err(e) => {
                    return Err(format!("save image failed. {:?}", e));
                }
                Ok(res) => {
                    fi.write_all(res.avif_file.as_slice());
                }
            }
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

1 participant