use clap::Parser; use image::GenericImageView; use show_image::{AsImageView, create_window, event}; use std::error::Error; #[derive(Parser)] #[command(name = "remove_background")] #[command(about = "Download and display an image (future: background removal)", long_about = None)] struct Args { #[arg(help = "URL of the image to download and display")] url: String, } #[show_image::main] fn main() -> Result<(), Box> { // Parse command line arguments let args = Args::parse(); println!("Downloading image from: {}", args.url); // Download the image with a user agent (using blocking client) let client = reqwest::blocking::Client::builder() .user_agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36") .build()?; let response = client.get(&args.url).send()?; if !response.status().is_success() { return Err(format!("Failed to download image: HTTP {}", response.status()).into()); } let bytes = response.bytes()?; println!("Downloaded {} bytes", bytes.len()); // Load the image let img = image::load_from_memory(&bytes)?; let (width, height) = img.dimensions(); println!("Loaded image: {}x{}", width, height); // Create a window and display the image. let window = create_window("image", Default::default())?; window.set_image( "downloaded image", &img.as_image_view().map_err(|e| e.to_string())?, )?; println!("Image displayed. Press ESC to close the window."); // Event loop - wait for ESC key to close for event in window.event_channel()? { if let event::WindowEvent::KeyboardInput(event) = event { if event.input.key_code == Some(event::VirtualKeyCode::Escape) && event.input.state.is_pressed() { println!("ESC pressed, closing..."); break; } } } Ok(()) }