image_compressor 1.5.3

A image compressing module using mozjpeg, and image crates.
Documentation

Image Compressor

Crates.io Documentation CI

A library for resizing and compressing images to jpg.

Features

  • Compress and resize a single image to jpg format.
  • Multithreading.
  • Customize the quality and size ratio of compressed images.
  • Send a completion message via mpsc::Sender (see Using Message Passing to Transfer Data Between Threads in rust tutorial).
  • Images of any resolution are decoded by default. Call set_memory_limit to cap decoder allocations when the source images are untrusted.

Supported Image Format

Visit image crate page. This crate uses image crate for opening image files.

Examples

FolderCompressor and its compress function example.

The function will compress all images, using multithreading, in a given source folder and will wait until everything is done. If user set a Sender for FolderCompressor, the method sends messages whether compressing is complete.

use std::path::PathBuf;
use std::sync::mpsc;
use image_compressor::FolderCompressor;
use image_compressor::Factor;

let source = PathBuf::from("source_dir");   // source directory path
let dest = PathBuf::from("dest_dir");       // destination directory path
let thread_count = 4;                       // number of threads
let (tx, tr) = mpsc::channel();             // Sender and Receiver. for more info, check mpsc and message passing. 

let mut comp = FolderCompressor::new(source, dest);
comp.set_factor(Factor::new(80., 0.8));
comp.set_thread_count(4);
comp.set_sender(tx);

match comp.compress(){
    Ok(_) => {},
    Err(e) => println!("Cannot compress the folder!: {}", e),
}

Compressor and compress_to_jpg example.

Compressing just a one image.

use std::path::PathBuf;
use image_compressor::compressor::Compressor;
use image_compressor::Factor;

let source = PathBuf::from("source").join("file1.jpg");
let dest = PathBuf::from("dest");

let mut comp = Compressor::new(source, dest);
comp.set_factor(Factor::new(80., 0.8));
comp.compress_to_jpg();