9 releases (stable)

Uses new Rust 2021

1.2.3 Nov 20, 2022
1.2.2 May 31, 2022
1.2.1 Apr 23, 2022
1.1.0 Mar 30, 2022
0.1.1 Mar 22, 2022

#85 in Images

Download history 4/week @ 2022-08-18 10/week @ 2022-08-25 5/week @ 2022-09-01 29/week @ 2022-09-08 4/week @ 2022-09-15 8/week @ 2022-09-22 21/week @ 2022-09-29 7/week @ 2022-10-06 2/week @ 2022-10-13 4/week @ 2022-10-20 7/week @ 2022-10-27 9/week @ 2022-11-03 9/week @ 2022-11-10 61/week @ 2022-11-17 17/week @ 2022-11-24 12/week @ 2022-12-01

101 downloads per month

MIT license

47KB
881 lines

Image Compressor

Crates.io Documentation

A library for resize and compress images to jpg.

Features

  • Compress and resize a single image to jpg format.
  • Compress and resize multiple images of certain directory.
  • Compress and resize images with multithreading.
  • Customize the quality and size of compressed images.
  • Send a completion message via mpsc::Sender (see Using Message Passing to Transfer Data Between Threads in rust tutorial).

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 compress all images in given origin folder with multithread at the same time, and 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 origin = PathBuf::from("origin_dir");   // original 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(origin, dest);
comp.set_cal_func(|width, height, file_size| {return Factor::new(75., 0.7)});
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 origin = PathBuf::from("origin").join("file1.jpg");
let dest = PathBuf::from("dest");
let comp = Compressor::new(origin_dir, dest_dir, |width, height, file_size| {return Factor::new(75., 0.7)});
comp.compress_to_jpg();

Dependencies

~17MB
~226K SLoC