Skip to content

Commit

Permalink
Création automatique du contrat
Browse files Browse the repository at this point in the history
  • Loading branch information
ineiti committed Jan 10, 2024
1 parent bdf253f commit a2db099
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 40 deletions.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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...

Expand All @@ -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:
Expand Down
File renamed without changes.
87 changes: 55 additions & 32 deletions scripts/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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.")
write_contract(get_image_strings(backgrounds, shoes, soles))

0 comments on commit a2db099

Please sign in to comment.