image icon indicating copy to clipboard operation
image copied to clipboard

Error: The encoder or decoder for Jpeg does not support the color type `Rgba8`

Open voratham opened this issue 1 year ago • 4 comments

I want to use the DynamicImage.thumbnail method but I got the error Error: The encoder or decoder for Jpeg does not support the color type Rgba8".

Here is an example of the code below:

use image::io::Reader as ImageReader;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let img_original = ImageReader::open("02.png")?.decode()?;

    img_original.thumbnail(100, 100);
    img_original.save("02.jpg")?;

    Ok(())
}

My Cargo.toml

[package]
name = "demo_image_lib"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.82"
image = "0.25.1"
tokio = { version = "1.37.0", features = ["full"] }

voratham avatar Apr 21 '24 07:04 voratham

The JPEG file format doesn't support alpha, but your image has an alpha channel. You can either remove the alpha channel (via to_rgb8) or use a different file format that does support alpha. Previous versions of this library used to silently drop the alpha channel

fintelia avatar Apr 21 '24 22:04 fintelia

does anybody know how to handle this when opening an image file?

there are crazy maniacs who has rgba8 in JPEG image header

hwkim1127 avatar May 30 '24 08:05 hwkim1127

@hwkim1127 That sounds like a different problem. You may want to open a dedicated issue and include an example failing image

fintelia avatar Jun 02 '24 19:06 fintelia

@hwkim1127 That sounds like a different problem. You may want to open a dedicated issue and include an example failing image

I apologize for previous comment. my code had an error where I was saving png to jpg directly without to_rgb8()

hwkim1127 avatar Jun 07 '24 16:06 hwkim1127