Closed lanyeeee closed 3 months ago
Save internally uses a BufWriter
which performs better than a raw File
object: https://github.com/image-rs/image/blob/d48c6a6ff310b356018ffd6b5daff0d73587c039/src/io/free_functions.rs#L51
It then internally creates a JpegEncoder, so that part is the same: https://github.com/image-rs/image/blob/d48c6a6ff310b356018ffd6b5daff0d73587c039/src/io/free_functions.rs#L72
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.
let file = fs::File::create("encoder_save_out.jpg")?;
let mut buffered_file_writer = &mut BufWriter::new(file);
let encoder = JpegEncoder::new_with_quality(&mut buffered_file_writer, 95);
in_img.write_with_encoder(encoder)?;
I needed to set the quality before save the jpg image, and I found the solution here https://github.com/image-rs/image/issues/1842#issuecomment-1683453765 (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?