DEV Community

Discussion on: NFT images generator using Python Jupyter Notebook

Collapse
 
victorquanlam profile image
Victor Quan Lam

There're 2 places you need to declare the colour

# Each image is made up a series of traits
# The weightings for each trait drive the rarity and add up to 100%

background = ["Blue", "Orange"] 
background_weights = [30, 70]

circle = ["Blue", "Orange"] 
circle_weights = [30, 70]

square = ["Blue","Orange"] 
square_weights = [30, 70]

# Dictionary variable for each trait. 
# Eech trait corresponds to its file name
# Add more shapes and colours as you wish

background_files = {
    "Blue": "blue",
    "Orange": "orange",
}
Enter fullscreen mode Exit fullscreen mode


`
If you have done it but still getting error, could you please let me have a look at your folder/file structure?

Collapse
 
gkc98 profile image
gkc98

background_files = {"blue": "blue", "green":"green", "LG": "LG", "pink": "pink", "purple": "purple", "orange": "orange"}
eyes_files = {
"I1": "I1", "I2": "I2", "I3": "I3", "I4": "I4", "I5": "I5", "I6": "I6","I7": "I7","I8": "I8","I9": "I9", "I10": "I10","I11": "I11", "I12": "I12", "I13": "I13", "I14": "I14", "I15": "I15", "I16": "I16"}
mouth_files = {"M1": "M1", "M2": "M2","M3": "M3", "M4": "M4", "M5": "M5", "M6":"M6", "M7": "M7", "M8": "M8", "M9": "M9", "M10": "M10", "M11": "M11"}
spoon_files = {"S1":"S1", "S2":"S2", "S3": "S3", "S4": "S4", "S5": "S5", "S6": "S6", "S7":"S7", "S8": "S8", "S9": "S9", "S10": "S10", "S11": "S11", "S12": "S12", "S13": "S13", "S14": "S14", "S15": "S15", "S16":"S16"}

so did this to my files and intead of of layers,i changes it to images
m1 = Image.open(f'./images/background/{background_files[item["background"]]}.png').convert('RGBA')

this fixed my intial problem.but i now have anew one :

10 com2 = Image.alpha_composite(com1, im3)
11 com3 = Image.alpha_composite(com2, im4)
12

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/PIL/Image.py in alpha_composite(im1, im2)
3042 im1.load()
3043 im2.load()
-> 3044 return im1._new(core.alpha_composite(im1.im, im2.im))
3045
3046

ValueError: images do not match

Thread Thread
 
victorquanlam profile image
Victor Quan Lam

Looks like it cannot find the images with the given names. Can you show me the declaration of im2 im3 and im4 as well? Thanks

Thread Thread
 
Sloan, the sloth mascot
Comment deleted
 
gkc98 profile image
gkc98

here they are, thank you so much for all your help. i really appreciate it

im1 = Image.open(f'./images/background/{background_files[item["background"]]}.png').convert('RGBA')
im2 = Image.open(f'./images/mouth/{mouth_files[item["mouth"]]}.png').convert('RGBA')
im3 = Image.open(f'./images/spoon/{spoon_files[item["spoon"]]}.png').convert('RGBA')
im4 = Image.open(f'./images/eyes/{eyes_files[item["eyes"]]}.png').convert('RGBA')

#Create each composite
com1 = Image.alpha_composite(im1, im2)
com2 = Image.alpha_composite(com1, im3)
com3 = Image.alpha_composite(com2, im4)
#Convert to RGB
rgb_im = com3.convert('RGB')
file_name = str(item["tokenId"]) + ".png"
rgb_im.save("./images/" + file_name)
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
gkc98 profile image
gkc98

hi victor, thanks for all your help. I realised that one of my images was not the same dimension as the other. i think that created the error. i've since resolved it and managed to generate the little images! thanks so much for the speedy replies and all your help!

Thread Thread
 
victorquanlam profile image
Victor Quan Lam

Congrats! You got it. Thanks for reading my blogs. I'm about to upload another post soon. Please consider to check it out. Thanks again