Skip to content

Commit

Permalink
Added create_artwork function
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-rt committed Aug 1, 2023
1 parent d7f26e8 commit a27c550
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/target
/inputs
/outputs
/assets
/metadata
.DS_Store
.DS_Store
76 changes: 44 additions & 32 deletions src/cli/generate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{Parser, ValueHint};
use console::style;
use image::imageops::overlay;
use image::{imageops, DynamicImage};
use rand::distributions::WeightedIndex;
use rand::prelude::*;
use rand::seq::SliceRandom;
Expand All @@ -26,24 +26,24 @@ pub struct GenerateArgs {
/// Number of assets to generate
#[clap(short, long)]
count: u128,

/// Input directory
#[clap(short, long, value_hint = ValueHint::FilePath, value_name = "INPUT PATH", default_value = ASSETS_INPUT)]
input: PathBuf,

input_dir: PathBuf,
/// Output assets directory
#[clap(short, long, value_hint = ValueHint::FilePath, value_name = "ASSETS OUTPUT PATH", default_value = ASSETS_OUTPUT)]
assets: PathBuf,

assets_dir: PathBuf,
/// Output metadata directory
#[clap(short, long, value_hint = ValueHint::FilePath, value_name = "METADATA OUTPUT PATH", default_value = METADATA_OUTPUT)]
metadata: PathBuf,
metadata_dir: PathBuf,
}

impl Cmd for GenerateArgs {
fn run(self) -> eyre::Result<()> {
let GenerateArgs {
count,
input,
assets,
metadata,
input_dir,
assets_dir,
metadata_dir,
} = self;

println!(
Expand All @@ -52,30 +52,30 @@ impl Cmd for GenerateArgs {
PALETTE_EMOJI
);

if !input.exists() {
if !input_dir.exists() {
// TODO: Deal with these unwraps
let mut cwd: String = env::current_dir()?.to_str().unwrap().to_owned();
cwd.push_str("/");
cwd.push_str(input.to_str().unwrap());
cwd.push_str(input_dir.to_str().unwrap());

eyre::bail!("Directory {:?} does not exist", cwd)
}

let trait_layers = load_layers(input)?;
let trait_layers = load_layers(input_dir)?;

// Create the assets output folder if it does not exist
if !assets.exists() {
// fs::create_dir_all(&assets)?;
if !assets_dir.exists() {
fs::create_dir_all(&assets_dir)?;
}

// Create the metadata output folder if it does not exist
if !metadata.exists() {
// fs::create_dir_all(&metadata)?;
if !metadata_dir.exists() {
fs::create_dir_all(&metadata_dir)?;
}

let trait_layer_keys: Vec<String> = trait_layers.keys().cloned().collect();
println!("{:?}", trait_layer_keys);

for _ in 0..count {
for i in 0..count {
let selected_layers: Vec<&Box<Layer>> = trait_layer_keys
.iter()
.map(|trait_type| {
Expand All @@ -92,9 +92,9 @@ impl Cmd for GenerateArgs {
.map(|l| l.unwrap())
.collect();

println!("{:?}", selected_layers);

let asset = create_artwork(&selected_layers);
println!("Creating id {i}");
let asset = create_artwork(&selected_layers)?;
asset.save(format!("{}/{}.png", assets_dir.to_str().unwrap(), i))?;
}
todo!()
}
Expand Down Expand Up @@ -125,9 +125,27 @@ impl Layer {
}
}

fn create_artwork(layers: &[&Box<Layer>]) {
type FinalImage = DynamicImage;

fn create_artwork(layers: &[&Box<Layer>]) -> eyre::Result<FinalImage> {
// TODO: Add error handling rather than unwrap
let base_layer = &layers.first().unwrap();
let canvas = &layers.first().unwrap();

let mut canvas = image::open(&canvas.value)
.map_err(|_| DirError::FileNotFoundError("Failed to open file".to_string()))?;

// Skip the first element (the base layer)
for layer in layers.iter().skip(1) {
let layer = image::open(&layer.value)
.map_err(|_| DirError::FileNotFoundError("Failed to open file".to_string()))?;
imageops::overlay(&mut canvas, &layer, 0, 0)
}

Ok(canvas)
}

fn create_metadata(layers: &[&Box<Layer>]) -> eyre::Result<()> {
todo!()
}

fn encode_combination(layers: &[&Box<Layer>]) -> eyre::Result<String> {
Expand Down Expand Up @@ -161,20 +179,14 @@ fn load_layers(input_dir: PathBuf) -> eyre::Result<TraitLayers> {
let mut subdir_layers: Vec<Box<Layer>> = vec![];

for file in subdir {
let file = file?.file_name();

let file_path = Path::new(&file);

let trait_value = file_path.file_stem().ok_or(DirError::FileStemError(
"Error reading file stem.".to_string(),
))?;
let trait_value = file?.path();

let rarity = 1;

// Cloning since I need trait_type later as well
let trait_type = trait_type.clone();

let layer = Box::new(Layer::new(trait_type, trait_value.into(), rarity));
let layer = Box::new(Layer::new(trait_type, trait_value, rarity));

subdir_layers.push(layer);
}
Expand Down
4 changes: 3 additions & 1 deletion src/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl Dir {
info!("Reading current path {path:?}");

// Get the subdirectories of the input folder
let mut contents = fs::read_dir(&path)?
let contents = fs::read_dir(&path)?
.map(|result| result.map(|item| item.path()))
.filter(|c| c.as_ref().unwrap().is_dir())
.collect::<Result<Vec<PathBuf>, _>>()?;
Expand All @@ -31,4 +31,6 @@ impl Dir {
pub enum DirError {
#[error("failed to get file stem: {0}")]
FileStemError(String),
#[error("failed to open file: {0}")]
FileNotFoundError(String),
}

0 comments on commit a27c550

Please sign in to comment.