Feature export unique tiles

This commit is contained in:
2023-10-19 23:51:38 +02:00
parent 4789c04b3d
commit 5768681a88

View File

@@ -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::<Result<Vec<_>, _>>()?;
// 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::<HashSet<_>>();
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::<String>();
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();