From 5768681a8890b582252117bbbb4d2e206d13f30f Mon Sep 17 00:00:00 2001 From: structix Date: Thu, 19 Oct 2023 23:51:38 +0200 Subject: [PATCH] Feature export unique tiles --- src/main.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main.rs b/src/main.rs index 5bb9a30..01d16c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -121,6 +121,15 @@ struct Opt { /// Keep the image's aspect ratio #[structopt(short, long)] keep_aspect_ratio: bool, + + /// Export all resized unique tiles seperately + #[structopt(long)] + unique_tiles: bool, + + /// Specify export directory of unique tiles export + #[structopt(long, default_value = "unique_tiles")] + unique_tiles_dir: PathBuf, + } fn main() -> Result<()> { @@ -131,6 +140,8 @@ fn main() -> Result<()> { tile_size, keep_aspect_ratio, output_dir, + unique_tiles, + unique_tiles_dir } = Opt::from_args(); fs::create_dir_all(&output_dir)?; @@ -139,6 +150,7 @@ fn main() -> Result<()> { let input_dir = fs::read_dir(input_dir)?.collect::, _>>()?; + // Run mosaic procedure for every cover image for image in input_dir { let input_path = image.path(); eprintln!("Processing {}", input_path.display()); @@ -147,10 +159,14 @@ fn main() -> Result<()> { "{}.mosaic{mosaic_size}.png", input_path.file_stem().unwrap().to_string_lossy() )); + + // TODO: decide if we'd like to override the previously generated mosaic if output.exists() { + println!("Mosaic {} already exists. Skipping.", output.display()); continue; } + // Open the mosaic cover image let img = image::open(&input_path)?; let img = if keep_aspect_ratio { img.thumbnail(mosaic_size, mosaic_size) @@ -164,6 +180,7 @@ fn main() -> Result<()> { .map(|(_x, _y, pixel)| pixel) .collect::>(); let len = unique_pixels.len(); + // Create a mapping between pixel and tile let tiles = unique_pixels .into_par_iter() .progress_with(make_pbar("pixels", len as _)) @@ -182,6 +199,17 @@ fn main() -> Result<()> { mosaic.copy_from(&**tiles.get(&pixel).unwrap(), x * tile_size, y * tile_size)?; } + // Optional export of unique tiles + if unique_tiles { + // Export tiles + for (pixel, tile) in tiles.iter().progress_with(make_pbar("export tiles", len as _)) { + // Create a filename from pixel value + let name = pixel.0.iter().map(|u| u.to_string()).collect::(); + let unique_filename = unique_tiles_dir.join(format!("{name}.png")); + let _ = tile.save(unique_filename)?; + } + } + let spinner = make_spinner("Saving", "Saved!"); mosaic.save(output)?; spinner.finish_using_style();