feat: add -o argument

This commit is contained in:
PurpleMyst
2020-07-05 13:37:48 +02:00
parent e3c2dca327
commit 57347238e4
3 changed files with 17 additions and 11 deletions

2
Cargo.lock generated
View File

@@ -548,7 +548,7 @@ dependencies = [
[[package]] [[package]]
name = "themis" name = "themis"
version = "1.0.0" version = "1.2.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"image", "image",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "themis" name = "themis"
version = "1.1.0" version = "1.2.0"
authors = ["PurpleMyst <PurpleMyst@users.noreply.github.com>"] authors = ["PurpleMyst <PurpleMyst@users.noreply.github.com>"]
license = "GPL-3.0-only" license = "GPL-3.0-only"
edition = "2018" edition = "2018"

View File

@@ -88,6 +88,10 @@ struct Opt {
#[structopt(parse(from_os_str))] #[structopt(parse(from_os_str))]
tiles_directory: PathBuf, tiles_directory: PathBuf,
/// Where to save the finished mosaic
#[structopt(short, long, parse(from_os_str), default_value = "mosaic.png")]
output: PathBuf,
/// The side length that the image to turn into to a mosaic will be resized to /// The side length that the image to turn into to a mosaic will be resized to
#[structopt(short, long, default_value = "128")] #[structopt(short, long, default_value = "128")]
mosaic_size: u32, mosaic_size: u32,
@@ -103,6 +107,7 @@ fn main() -> Result<()> {
tiles_directory, tiles_directory,
mosaic_size, mosaic_size,
keep_aspect_ratio, keep_aspect_ratio,
output,
} = Opt::from_args(); } = Opt::from_args();
let possible_tiles = load_images(tiles_directory)?; let possible_tiles = load_images(tiles_directory)?;
@@ -114,25 +119,26 @@ fn main() -> Result<()> {
}; };
// For every unique pixel in the image, find its most appropiate tile // For every unique pixel in the image, find its most appropiate tile
let unique_pixels = image.pixels().collect::<HashSet<_>>(); let unique_pixels = image
.pixels()
.map(|(_x, _y, pixel)| pixel)
.collect::<HashSet<_>>();
let pbar = make_pbar("pixels", unique_pixels.len() as _); let pbar = make_pbar("pixels", unique_pixels.len() as _);
let tiles = unique_pixels let tiles = unique_pixels
.into_par_iter() .into_par_iter()
.progress_with(pbar) .progress_with(pbar)
.filter_map(|(x, y, pixel)| { .filter_map(|pixel| {
let pixel = pick_image_for_pixel(pixel, &possible_tiles)?; let tile = pick_image_for_pixel(pixel, &possible_tiles)?;
Some(((x, y), pixel)) Some((pixel, tile))
}) })
.collect::<HashMap<_, _>>(); .collect::<HashMap<_, _>>();
// Apply the mapping previously calculated and save the mosaic // Apply the mapping previously calculated and save the mosaic
let mut mosaic = DynamicImage::new_rgba8(image.width() * TILE_SIDE, image.height() * TILE_SIDE); let mut mosaic = DynamicImage::new_rgba8(image.width() * TILE_SIDE, image.height() * TILE_SIDE);
for y in 0..image.height() { for (x, y, pixel) in image.pixels() {
for x in 0..image.width() { mosaic.copy_from(&**tiles.get(&pixel).unwrap(), x * TILE_SIDE, y * TILE_SIDE)?;
mosaic.copy_from(*tiles.get(&(x, y)).unwrap(), x * TILE_SIDE, y * TILE_SIDE)?;
}
} }
mosaic.save("mosaic.png")?; mosaic.save(output)?;
Ok(()) Ok(())
} }