You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I needed to set the quality before save the jpg image, and I found the solution here #1842 (comment) (use image::codecs::jpeg::JpegEncoder), but I found it very slow, saving a jpg image with it took almost 10 times as long as ImageBuffer::save.
I also tried jpeg-encoder. In my test, it takes 1/2 the time of ImageBuffer::save to encode an image, here's my test code.
main.rs
use anyhow::{Ok,Result};use image::codecs::jpeg::JpegEncoder;use std::fs;fnmain() -> Result<()>{let in_img = image::open("1.jpg").expect("Failed to open output image").to_rgb8();// save with image::codecs::jpeg::JpegEncoderlet timer = std::time::Instant::now();letmut writer = fs::File::create("encoder_save_out.jpg")?;let encoder = JpegEncoder::new_with_quality(&mut writer,95);
in_img.write_with_encoder(encoder)?;println!("write_with_encoder: {:?}", timer.elapsed());// save with ImageBuffer::savelet timer = std::time::Instant::now();
in_img.save("save_out.jpg")?;println!("ImageBuffer::save: {:?}", timer.elapsed());// save with jpeg_encoderlet timer = std::time::Instant::now();let encoder = jpeg_encoder::Encoder::new_file("jpeg_encoder_out.jpg",95)?;
encoder.encode(&in_img.as_raw(),
in_img.width()asu16,
in_img.height()asu16,
jpeg_encoder::ColorType::Rgb,)?;println!("jpeg_encoder: {:?}", timer.elapsed());Ok(())}
I'm really sorry for didn't consider the File is not buffered.
Without buffering, each write is a system call, so it's very slow.
Now I've changed the code like this, it works like a charm.
I needed to set the quality before save the jpg image, and I found the solution here #1842 (comment) (use
image::codecs::jpeg::JpegEncoder
), but I found it very slow, saving a jpg image with it took almost 10 times as long asImageBuffer::save
.I also tried jpeg-encoder. In my test, it takes 1/2 the time of
ImageBuffer::save
to encode an image, here's my test code.main.rs
Cargo.toml
output
So why is using
image::codecs::jpeg::JpegEncoder
so much slower thanImageBuffer::save
, is there some bug?The text was updated successfully, but these errors were encountered: