Function turbojpeg::compress_image
source · pub fn compress_image<P>(
image_buf: &ImageBuffer<P, Vec<u8>>,
quality: i32,
subsamp: Subsamp
) -> Result<OwnedBuf>where
P: JpegPixel + 'static,
Expand description
Compresses an image::ImageBuffer
into JPEG.
quality
controls the tradeoff between image quality and size of the compressed image. It
ranges from 1 (worst quality, smallest size) to 100 (best quality, largest size).
subsamp
sets the level of chrominance subsampling of the compressed JPEG image (please see
the documentation of Subsamp
for details). Use Subsamp::None
for no subsampling
(highest quality).
§Example
// create an `image::RgbImage`
let image = image::RgbImage::from_fn(256, 256, |x, y| image::Rgb([x as u8, y as u8, 128]));
// compress `image` into JPEG with quality 95 and 2x2 chrominance subsampling
let jpeg_data = turbojpeg::compress_image(&image, 95, turbojpeg::Subsamp::Sub2x2)?;
// write the JPEG to disk
std::fs::write(std::env::temp_dir().join("gradient.jpg"), &jpeg_data)?;