Skip to content
This repository has been archived by the owner on Oct 28, 2023. It is now read-only.

Add --flips-and-rotates flag #57

Merged
merged 6 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use structopt::StructOpt;

use std::path::PathBuf;
use texture_synthesis::{
image::ImageOutputFormat as ImgFmt, Dims, Error, Example, ImageSource, SampleMethod, Session,
image::ImageOutputFormat as ImgFmt, load_dynamic_image, Dims, Error, Example, ImageSource,
SampleMethod, Session,
};

fn parse_size(input: &str) -> Result<Dims, std::num::ParseIntError> {
Expand Down Expand Up @@ -60,6 +61,20 @@ struct TransferStyle {
guide: PathBuf,
}

#[derive(StructOpt)]
#[structopt(rename_all = "kebab-case")]
struct FlipsAndRotates {
/// A target guidance map
#[structopt(long, parse(from_os_str))]
target_guide: Option<PathBuf>,
/// Path(s) to guide maps for the example output.
#[structopt(long = "guides", parse(from_os_str))]
example_guides: Vec<PathBuf>,
/// Path(s) to example images used to synthesize a new image
#[structopt(parse(from_os_str))]
examples: Vec<PathBuf>,
}

#[derive(StructOpt)]
enum Subcommand {
/// Transfers the style from an example onto a target guide
Expand All @@ -68,6 +83,10 @@ enum Subcommand {
/// Generates a new image from 1 or more examples
#[structopt(name = "generate")]
Generate(Generate),
/// Generates a new image from 1 or more examples, extended with their
/// flipped and rotated versions
#[structopt(name = "flips-and-rotates")]
FlipsAndRotates(FlipsAndRotates),
}

#[derive(StructOpt)]
Expand Down Expand Up @@ -215,6 +234,32 @@ fn real_main() -> Result<(), Error> {

(examples, gen.target_guide.as_ref())
}
Subcommand::FlipsAndRotates(fr) => {
let mut examples: Vec<_> = fr.examples.iter().map(Example::new).collect();
if !fr.example_guides.is_empty() {
for (ex, guide) in examples.iter_mut().zip(fr.example_guides.iter()) {
ex.with_guide(guide);
}
}

let mut transformed_examples: Vec<_> = vec![];
for example_path in &fr.examples {
let base_image = load_dynamic_image(example_path.into())?;
let mut new_examples: Vec<_> = vec![
Example::new(base_image.fliph()),
Example::new(base_image.rotate90()),
Example::new(base_image.fliph().rotate90()),
Example::new(base_image.rotate180()),
Example::new(base_image.fliph().rotate180()),
Example::new(base_image.rotate270()),
Example::new(base_image.fliph().rotate270()),
];
transformed_examples.append(&mut new_examples);
}
examples.append(&mut transformed_examples);

(examples, fr.target_guide.as_ref())
}
Subcommand::TransferStyle(ts) => (vec![Example::new(&ts.style)], Some(&ts.guide)),
};

Expand Down
1 change: 1 addition & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use std::path::Path;
mod unsync;

pub use image;
pub use utils::load_dynamic_image;
pub use utils::ImageSource;

pub use errors::Error;
Expand Down
15 changes: 9 additions & 6 deletions lib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ where
}
}

pub(crate) fn load_image(
src: ImageSource<'_>,
resize: Option<Dims>,
) -> Result<image::RgbaImage, Error> {
let img = match src {
pub fn load_dynamic_image(src: ImageSource<'_>) -> Result<image::DynamicImage, image::ImageError> {
match src {
ImageSource::Memory(data) => image::load_from_memory(data),
ImageSource::Path(path) => image::open(path),
ImageSource::Image(img) => Ok(img),
}?;
}
}

pub(crate) fn load_image(
src: ImageSource<'_>,
resize: Option<Dims>,
) -> Result<image::RgbaImage, Error> {
let img = load_dynamic_image(src)?;
Ok(match resize {
None => img.to_rgba(),
Some(ref size) => {
Expand Down