Add stdin and stdout support

This commit is contained in:
2021-10-27 20:56:42 +02:00
parent 66fc53d30f
commit 8731a4486e
2 changed files with 66 additions and 30 deletions

58
Cargo.lock generated
View File

@ -63,9 +63,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "clap" name = "clap"
version = "3.0.0-beta.4" version = "3.0.0-beta.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcd70aa5597dbc42f7217a543f9ef2768b2ef823ba29036072d30e1d88e98406" checksum = "feff3878564edb93745d58cf63e17b63f24142506e7a20c87a5521ed7bfb1d63"
dependencies = [ dependencies = [
"atty", "atty",
"bitflags", "bitflags",
@ -76,14 +76,14 @@ dependencies = [
"strsim", "strsim",
"termcolor", "termcolor",
"textwrap", "textwrap",
"vec_map", "unicase",
] ]
[[package]] [[package]]
name = "clap_derive" name = "clap_derive"
version = "3.0.0-beta.4" version = "3.0.0-beta.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b5bb0d655624a0b8770d1c178fb8ffcb1f91cc722cb08f451e3dc72465421ac" checksum = "8b15c6b4f786ffb6192ffe65a36855bc1fc2444bcd0945ae16748dcd6ed7d0d3"
dependencies = [ dependencies = [
"heck", "heck",
"proc-macro-error", "proc-macro-error",
@ -169,9 +169,9 @@ checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]] [[package]]
name = "gif" name = "gif"
version = "0.11.2" version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a668f699973d0f573d15749b7002a9ac9e1f9c6b220e7b165601334c173d8de" checksum = "c3a7187e78088aead22ceedeee99779455b23fc231fe13ec443f99bb71694e5b"
dependencies = [ dependencies = [
"color_quant", "color_quant",
"weezl", "weezl",
@ -247,9 +247,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.103" version = "0.2.105"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8f7255a17a627354f321ef0055d63b898c6fb27eff628af4d1b66b7331edf6" checksum = "869d572136620d55835903746bcb5cdc54cb2851fd0aeec53220b4bb65ef3013"
[[package]] [[package]]
name = "lockscreen-blur" name = "lockscreen-blur"
@ -260,6 +260,12 @@ dependencies = [
"image", "image",
] ]
[[package]]
name = "memchr"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]] [[package]]
name = "memoffset" name = "memoffset"
version = "0.6.4" version = "0.6.4"
@ -341,9 +347,12 @@ dependencies = [
[[package]] [[package]]
name = "os_str_bytes" name = "os_str_bytes"
version = "3.1.0" version = "4.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6acbef58a60fe69ab50510a55bc8cdd4d6cf2283d27ad338f54cb52747a9cf2d" checksum = "addaa943333a514159c80c97ff4a93306530d965d27e139188283cd13e06a799"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "png" name = "png"
@ -383,18 +392,18 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.29" version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9f5105d4fdaab20335ca9565e106a5d9b82b6219b5ba735731124ac6711d23d" checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43"
dependencies = [ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.9" version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
@ -444,9 +453,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.77" version = "1.0.81"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5239bc68e0fef57495900cfea4e8dc75596d9a319d7e16b1e0a440d24e6fe0a0" checksum = "f2afee18b8beb5a596ecb4a2dce128c719b4ba399d34126b9e4396e3f9860966"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -482,6 +491,15 @@ dependencies = [
"weezl", "weezl",
] ]
[[package]]
name = "unicase"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
dependencies = [
"version_check",
]
[[package]] [[package]]
name = "unicode-segmentation" name = "unicode-segmentation"
version = "1.8.0" version = "1.8.0"
@ -500,12 +518,6 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.3" version = "0.9.3"

View File

@ -1,18 +1,22 @@
use clap::Clap; use clap::Parser;
use image::imageops::overlay; use image::imageops::overlay;
use image::imageops::FilterType; use image::imageops::FilterType;
use image::io::Reader as ImageReader; use image::io::Reader as ImageReader;
use image::{DynamicImage, ImageOutputFormat}; use image::{DynamicImage, ImageOutputFormat};
use std::io::{self, Cursor, Read};
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Clap)] #[derive(Parser)]
struct Opt { struct Opt {
/// The path to the input image, if not specified will read from stdin
#[clap(short, long)] #[clap(short, long)]
input: PathBuf, input: Option<PathBuf>,
/// The path to the output image, if not specified will write to stdout
#[clap(short, long)] #[clap(short, long)]
output: PathBuf, output: Option<PathBuf>,
/// An optional path to an additional image to overlay on top of the output image
#[clap(short, long)] #[clap(short, long)]
stamp: Option<PathBuf>, stamp: Option<PathBuf>,
@ -22,7 +26,19 @@ struct Opt {
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let opt = Opt::parse(); let opt = Opt::parse();
let img = ImageReader::open(&opt.input)?.decode()?;
let img = match &opt.input {
Some(path) => ImageReader::open(path)?.decode()?,
None => {
let mut data = vec![];
let stdin = io::stdin();
stdin.lock().read_to_end(&mut data)?;
ImageReader::new(Cursor::new(&data))
.with_guessed_format()?
.decode()?
}
};
let (w, h) = dimensions(&img); let (w, h) = dimensions(&img);
let img = img.resize(w / opt.blur, h / opt.blur, FilterType::Triangle); let img = img.resize(w / opt.blur, h / opt.blur, FilterType::Triangle);
let mut img = img.resize(w, h, FilterType::Nearest); let mut img = img.resize(w, h, FilterType::Nearest);
@ -33,8 +49,16 @@ fn main() -> anyhow::Result<()> {
overlay(&mut img, &stamp, (w - sw) / 2, (h - sh) / 2); overlay(&mut img, &stamp, (w - sw) / 2, (h - sh) / 2);
} }
let mut output = std::fs::File::create(opt.output)?; match &opt.output {
img.write_to(&mut output, ImageOutputFormat::Png)?; Some(path) => {
let mut output = std::fs::File::create(path)?;
img.write_to(&mut output, ImageOutputFormat::Png)?;
}
None => {
let stdout = io::stdout();
img.write_to(&mut stdout.lock(), ImageOutputFormat::Png)?;
}
}
Ok(()) Ok(())
} }