Allow reading image from stdin

This commit is contained in:
2025-07-14 18:36:21 +02:00
parent 15a47c7549
commit 3222f1b8be

View File

@ -7,13 +7,14 @@ use image::{
use nix::ioctl_write_ptr; use nix::ioctl_write_ptr;
use std::{ use std::{
fs::File, fs::File,
io::{Cursor, Read, stdin},
os::fd::AsRawFd, os::fd::AsRawFd,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
#[derive(Parser)] #[derive(Parser)]
struct Opt { struct Opt {
/// Path to the image-blob. /// Path to the image-blob. "-" means stdin.
pub path: PathBuf, pub path: PathBuf,
/// Stamp an image onto the final image. "<x>:<y>:<path>" /// Stamp an image onto the final image. "<x>:<y>:<path>"
@ -65,9 +66,15 @@ fn to_grayscale(path: &Path, image: DynamicImage) -> GrayAlphaImage {
} }
fn load_wallpaper(path: &Path) -> eyre::Result<GrayAlphaImage> { fn load_wallpaper(path: &Path) -> eyre::Result<GrayAlphaImage> {
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:?}"); eprintln!("Opening image at {path:?}");
ImageReader::open(path)?.decode()?
let mut image = ImageReader::open(path)?.decode()?; };
let image_dimensions = (image.width() as usize, image.height() as usize); let image_dimensions = (image.width() as usize, image.height() as usize);