From 3222f1b8be43b4258fb7be434010968eacf773d5 Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Mon, 14 Jul 2025 18:36:21 +0200 Subject: [PATCH] Allow reading image from stdin --- src/main.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 88249e2..7395a19 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,13 +7,14 @@ use image::{ use nix::ioctl_write_ptr; use std::{ fs::File, + io::{Cursor, Read, stdin}, os::fd::AsRawFd, path::{Path, PathBuf}, }; #[derive(Parser)] struct Opt { - /// Path to the image-blob. + /// Path to the image-blob. "-" means stdin. pub path: PathBuf, /// Stamp an image onto the final image. "::" @@ -65,9 +66,15 @@ fn to_grayscale(path: &Path, image: DynamicImage) -> GrayAlphaImage { } fn load_wallpaper(path: &Path) -> eyre::Result { - eprintln!("Opening image at {path:?}"); - - let mut image = ImageReader::open(path)?.decode()?; + let mut image = if path == Path::new("-") { + eprintln!("Reading image from stdin"); + let mut buf = vec![]; + stdin().read_to_end(&mut buf)?; + ImageReader::new(Cursor::new(buf)).decode()? + } else { + eprintln!("Opening image at {path:?}"); + ImageReader::open(path)?.decode()? + }; let image_dimensions = (image.width() as usize, image.height() as usize);