Mercurial > image-resizer
view src/main.rs @ 1:1a362692ed76 default tip
Add native support for stdin/stdout.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 11 Jan 2022 16:25:21 +0100 |
parents | 9b4be43ea74f |
children |
line wrap: on
line source
use image::{imageops::FilterType, GenericImageView}; use log::info; use std::env; use std::error::Error; use std::fs::File; use std::io::{self, BufReader, Read}; use std::path::Path; use std::str::FromStr; fn main() -> Result<(), Box<dyn Error>> { let args: Vec<_> = env::args().collect(); if args.len() != 4 { eprintln!("Usage: {} <size> <input> <output>", args[0]); std::process::exit(1); } let size = u32::from_str(&args[1])?; let mut input = Vec::new(); if args[2] == "-" { io::stdin().read_to_end(&mut input)?; } else { let file = File::open(&args[2])?; let mut reader = BufReader::new(file); reader.read_to_end(&mut input)?; } let output = if args[3] == "-" { Path::new("/dev/stdout") } else { Path::new(&args[3]) }; // Guess the format of the input file. let format = image::guess_format(&input)?; // Decode the image file. let image = image::load_from_memory(&input)?; let (mut width, mut height) = image.dimensions(); if width > size { width = size; } if height > size { height = size; } // Resize it to the requested size. let image = image.resize(width, height, FilterType::Triangle); // Then save it to the output. image.save_with_format(output, format)?; info!("Successfully resized!"); Ok(()) }