Crate image_compressor
source ·Expand description
§Image compressor
image_compressor
is a library that compresses images with multiple threads.
See image crate for check the extension that supported.
If you want to compress a single image, see Compressor
struct.
Or if you want to compress multiple images in a certain directory, see FolderCompressor
struct.
It compresses images using multiple threads.
To use these structs and its functions, you need to give them a function pointer or closure
that calculate size and quality of new compressed images.
That calculator function(or closure) need to calculate and returns a Factor
base on image size and file size of the source image.
To see more information about it, see Factor
.
§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_dir = PathBuf::from("source").join("file1.jpg");
let dest_dir = PathBuf::from("dest");
let mut comp = Compressor::new(source_dir, dest_dir);
comp.set_factor(Factor::new(80., 0.8));
comp.compress_to_jpg();
Re-exports§
pub use compressor::Factor;
Modules§
- Module that contains things related to compressing an image.
- Containing functions that return a list of files or folders.
- Functions related to directory manipulation.
Structs§
- Compressor struct for a directory.