Skip to content

Files

Latest commit

 

History

History
34 lines (22 loc) · 916 Bytes

controlling_jpeg_quality.md

File metadata and controls

34 lines (22 loc) · 916 Bytes

Controlling JPEG Quality

We can manually control the quality of a JPEG image. We use JpegEncoder to help us specify the quality.

let encoder = JpegEncoder::new_with_quality(&mut file, 1);

The second parameter is the quality, which has a range between 1 (worst) and 100 (best).

The complete code is shown below:

use std::fs::File;
use image::codecs::jpeg::JpegEncoder;

fn main() {
    let img = image::open("my_image.jpg").unwrap();
    
    let mut file = File::create("jpeg_quality.jpg").unwrap();
    let encoder = JpegEncoder::new_with_quality(&mut file, 1);

    img.write_with_encoder(encoder).unwrap();
}

jpeg_quality.jpg:

jpeg_quality

➡️ Next: Creating GIF Animations

📘 Back: Table of contents