From a2db09941186fc640f068d15fecd73edce88a1e8 Mon Sep 17 00:00:00 2001 From: Linus Gasser Date: Wed, 10 Jan 2024 20:29:19 +0100 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20automatique=20du=20contrat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++--- mk_images.sh => mk_contract.sh | 0 scripts/images.py | 87 +++++++++++++++++++++------------- 3 files changed, 59 insertions(+), 40 deletions(-) rename mk_images.sh => mk_contract.sh (100%) diff --git a/README.md b/README.md index d859319..919e037 100644 --- a/README.md +++ b/README.md @@ -297,17 +297,13 @@ A la fin, il faut copier les images dans le smart contract. qui y sont déjà présentes 2. Ouvrez le fichier `./scripts/images.py` 3. Modifiez le nom des fichiers - faites attention au chemin d'accès -4. Lancez le script dans le terminal: +4. Lancez le script dans le terminal qui va créer le contrat `./contracts/SismondiNFT.sol`: ```bash -./mk_images.sh +./mk_contract.sh ``` -5. Il y a deux parties qu'il faut copier/coller dans le contrat `./contracts/SismondiNFT.sol`: - - La partie `Prefix` doit suivre la déclaration d'`images_prefix`. - Ne pas oublier le `;` à la fin! - - Les valeurs après le `Images are:` doivent remplacer ceux actuellement dans le - tableau de `images` +5. Vérifiez que les images résultantes sont bonnes en visualisant le répertoire `./images/results` 6. Vous pouvez aussi modifier la liste des mots qui est utilisé pour nommer vos NFTs. Essayez de montrer de la maturité dans le choix de vos mots... @@ -323,7 +319,7 @@ dans le contrat. Lancez la commande suivante: ```bash -./brownie.sh run sismondi.py deploy_mint --network development +./brownie.sh run sismondi_test.py --network development ``` Si tout va bien, vous devriez voir un total de 3 blocs: diff --git a/mk_images.sh b/mk_contract.sh similarity index 100% rename from mk_images.sh rename to mk_contract.sh diff --git a/scripts/images.py b/scripts/images.py index ce575ba..aa3c0a2 100755 --- a/scripts/images.py +++ b/scripts/images.py @@ -2,12 +2,21 @@ import io import os import base64 +import itertools image_dir = os.path.join(os.path.dirname(__file__), "..", "images") -def create_composite_image(file_paths, output_width, index): + +def calc_min_dimensions(image_names): + images = [Image.open(os.path.join(image_dir, image)) for image in image_names] + min_width = min([image.width for image in images]) + min_height = min([image.height for image in images]) + return min_width, min_height + + +def create_composite_image(file_paths, width, height, output_width, index): # Load the images - images = [Image.open(os.path.join(image_dir, fp)).convert("RGBA") for fp in file_paths] + images = [Image.open(os.path.join(image_dir, fp)).convert("RGBA").resize((width, height)) for fp in file_paths] # Superimpose the images one over the other composite = Image.alpha_composite(Image.alpha_composite(images[0], images[1]), images[2]) @@ -17,7 +26,7 @@ def create_composite_image(file_paths, output_width, index): new_height = int(output_width * aspect_ratio) resized_composite = composite.resize((output_width, new_height)) results_dir = os.path.join(image_dir, "results") - os.makedirs(results_dir, exist_ok = True) + os.makedirs(results_dir, exist_ok=True) resized_composite.save(os.path.join(results_dir, f"composite_{index}.png")) # Convert the image to base64 @@ -30,8 +39,48 @@ def create_composite_image(file_paths, output_width, index): def get_common(images): pre = os.path.commonprefix(images) - remaining_parts = ['"'+s[len(pre):] for s in images] - return pre+'"', remaining_parts + remaining_parts = ['"' + s[len(pre):] for s in images] + return pre + '"', remaining_parts + + +def get_image_strings(background_names, shoe_names, sole_names): + image_names = list(itertools.chain.from_iterable([background_names, shoe_names, sole_names])) + width, height = calc_min_dimensions(image_names) + print(f"Resizing all images to width={width}, height={height}") + # Uncomment the following lines to set the width and height to a given value + # width = 320 + # height = 240 + + # For-loop with all combinations of backgrounds / shoe / sole + images_long = [] + index = 0 + for background in background_names: + for shoe in shoe_names: + for sole in sole_names: + img_str = create_composite_image([background, shoe, sole], width, height, 30, index) + images_long.append(f"\"{img_str}\"") + index += 1 + + return images_long + + +def write_contract(images_long): + prefix, images_short = get_common(images_long) + + nft_parts_prefix = os.path.join(os.path.dirname(__file__), "SismondiNFT_") + with open(os.path.join(image_dir, "..", "contracts", "SismondiNFT.sol"), "w") as nft: + with open(f"{nft_parts_prefix}1.sol") as pre: + nft.write(pre.read()) + + nft.write(prefix) + with open(f"{nft_parts_prefix}2.sol") as middle: + nft.write(middle.read()) + + nft.write(",\n".join(images_short)) + with open(f"{nft_parts_prefix}3.sol") as end: + nft.write(end.read()) + + print("../contracts/SismondiNFT.sol has been rewritten.") # Example usage (replace 'file1.png', 'file2.png', 'file3.png' with actual file paths) @@ -48,30 +97,4 @@ def get_common(images): "sole/file2.png", ] -# For-loop with all combinations of backgrounds / shoe / sole -images_long = [] -index = 0 -for background in backgrounds: - for shoe in shoes: - for sole in soles: - img_str = create_composite_image([background, shoe, sole], 30, index) - images_long.append(f"\"{img_str}\"") - index += 1 - - -prefix, images_short = get_common(images_long) - -nft_parts_prefix = os.path.join(os.path.dirname(__file__), "SismondiNFT_") -with open(os.path.join(image_dir, "..", "contracts", "SismondiNFT.sol"), "w") as nft: - with open(f"{nft_parts_prefix}1.sol") as pre: - nft.write(pre.read()) - - nft.write(prefix) - with open(f"{nft_parts_prefix}2.sol") as middle: - nft.write(middle.read()) - - nft.write(",\n".join(images_short)) - with open(f"{nft_parts_prefix}3.sol") as end: - nft.write(end.read()) - -print("../contracts/SismondiNFT.sol has been rewritten.") \ No newline at end of file +write_contract(get_image_strings(backgrounds, shoes, soles))