随着时间的推移,数字藏品领域越来越火。当一个加密朋克头像以15万美元的价格出售给Visa时,人们逐渐通过各种渠道去认识NFT。
方法论
这个生成器背后的方法很简单。通过将不同的特征结合在一起,创建一个独特的头像。
获取你的数据
你将使用 usetech-llc
的 "Substrapunks "
资源库中的数据。
在下面的链接中下载他们的资源库,并将压缩文件解压到你的本地电脑上。
1. `https
:
//github.com/usetech-llc/substrapunks/archive/refs/heads/master.zip`
导入软件包
你将在这个项目中使用以下软件包:
- PIL
- IPython
- Random
- Json
- OS
1. `from PIL import Image`
2. `from IPython.display import display`
3. `import random`
4. `import json`
5. `import os`
指定NFT特性的稀有性
每个独特的头像都由五个特征组成。
- Face
- Ears
- Hair
- Mouth
- Nose
稀有性很重要,因为它创造了稀缺性,反过来又创造了价值。你将通过给一个特征中的不同类型分配权重来实现特征中的稀有性。权重的总和应该总是100。
有两种类型的脸(黑色和白色)。你在程序中可以规定,一张图片有60%的机会获得白脸,40%的机会获得黑脸。
1. `# Each image is made up a series of traits`
2. `# The weightings for each trait drive the rarity and add up to 100%`
3.
4. `face = ["White", "Black"]`
5. `face_weights = [60, 40]`
6.
7. `ears = ["No Earring", "Left Earring", "Right Earring", "Two Earrings"]`
8. `ears_weights = [25, 30, 44, 1]`
9.
10. `eyes = ["Regular", "Small", "Rayban", "Hipster", "Focused"]`
11. `eyes_weights = [70, 10, 5 , 1 , 14]`
12.
13. `hair = ['Up Hair', 'Down Hair', 'Mohawk', 'Red Mohawk', 'Orange Hair', 'Bubble Hair', 'Emo Hair',`
14. `'Thin Hair',`
15. `'Bald',`
16. `'Blonde Hair',`
17. `'Caret Hair',`
18. `'Pony Tails']`
19. `hair_weights = [10 , 10 , 10 , 10 ,10, 10, 10 ,10 ,10, 7 , 1 , 2]`
20.
21. `mouth = ['Black Lipstick', 'Red Lipstick', 'Big Smile', 'Smile', 'Teeth Smile', 'Purple Lipstick']`
22. `mouth_weights = [10, 10,50, 10,15, 5]`
23.
24. `nose = ['Nose', 'Nose Ring']`
25. `nose_weights = [90, 10]`
对特性进行分类
字典是用来将特征名称重定向到它们的文件名。你可以在以下位置找到特征文件名:
1. `...
\substrapunks
-
master\scripts\face\_parts\
。`
特性名称 "White "被引导到face1,而 "Black "被引导到face2。
1. `#Classify traits`
2.
3. `face_files = {`
4. `"White": "face1",`
5. `"Black": "face2"`
6. `}`
7.
8. `ears_files = {`
9. `"No Earring": "ears1",`
10. `"Left Earring": "ears2",`
11. `"Right Earring": "ears3",`
12. `"Two Earrings": "ears4"`
13. `}`
14.
15. `eyes_files = {`
16. `"Regular": "eyes1",`
17. `"Small": "eyes2",`
18. `"Rayban": "eyes3",`
19. `"Hipster": "eyes4",`
20. `"Focused": "eyes5"`
21. `}`
22.
23. `hair_files = {`
24. `"Up Hair": "hair1",`
25. `"Down Hair": "hair2",`
26. `"Mohawk": "hair3",`
27. `"Red Mohawk": "hair4",`
28. `"Orange Hair": "hair5",`
29. `"Bubble Hair": "hair6",`
30. `"Emo Hair": "hair7",`
31. `"Thin Hair": "hair8",`
32. `"Bald": "hair9",`
33. `"Blonde Hair": "hair10",`
34. `"Caret Hair": "hair11",`
35. `"Pony Tails": "hair12"`
36. `}`
37.
38.
39. `mouth_files = {`
40. `"Black Lipstick": "m1",`
41. `"Red Lipstick": "m2",`
42. `"Big Smile": "m3",`
43. `"Smile": "m4",`
44. `"Teeth Smile": "m5",`
45. `"Purple Lipstick": "m6"`
46. `}`
47.
48. `nose_files = {`
49. `"Nose": "n1",`
50. `"Nose Ring": "n2"`
51. `}`
定义图像特质
你要创建的每个头像都将是六张图片的组合:脸、鼻子、嘴、耳朵和眼睛。
因此,可以写一个for循环,将这些特征组合成一张图片,并指定图片的总数量。
一个函数为每张图片创建一个字典,指定它拥有哪些特征。
这些特征是根据 random.choice()
函数给出的。
这个函数遍历脸部特征列表(白色、黑色),并返回白色(60%的机会)或黑色(40%的机会)。
1. `## Generate Traits`
2.
3. `TOTAL_IMAGES = 100 # Number of random unique images we want to generate`
4.
5. `all_images = []`
6.
7. `# A recursive function to generate unique image combinations`
8. `def create_new_image():`
9.
10. `new_image = {} #`
11.
12. `# For each trait category, select a random trait based on the weightings`
13. `new_image ["Face"] = random.choices(face, face_weights)[0]`
14. `new_image ["Ears"] = random.choices(ears, ears_weights)[0]`
15. `new_image ["Eyes"] = random.choices(eyes, eyes_weights)[0]`
16. `new_image ["Hair"] = random.choices(hair, hair_weights)[0]`
17. `new_image ["Mouth"] = random.choices(mouth, mouth_weights)[0]`
18. `new_image ["Nose"] = random.choices(nose, nose_weights)[0]`
19.
20. `if new_image in all_images:`
21. `return create_new_image()`
22. `else:`
23. `return new_image`
24.
25.
26. `# Generate the unique combinations based on trait weightings`
27. `for i in range(TOTAL_IMAGES):`
28.
29. `new_trait_image = create_new_image()`
30.
31. `all_images.append(new_trait_image)`
验证唯一性
对于NFT头像项目来说,每个头像都是独一无二的,这一点很重要。因此,需要检查所有的图像是否是唯一的。写一个简单的函数,在所有的图像上循环,将它们存储到一个列表中,并返回重复的图像。
接下来,为每个图像添加一个唯一的标识符。
1. `# Returns true if all images are unique`
2. `def all_images_unique(all_images):`
3. `seen = list()`
4. `return not any(i in seen or seen.append(i) for i in all_images)`
5.
6. `print("Are all images unique?", all_images_unique(all_images))`
7. `# Add token Id to each image`
8. `i = 0`
9. `for item in all_images:`
10. `item["tokenId"] = i`
11. `i = i + 1`
12.
13. `print(all_images)`
性状计数
根据预定的权重和随机函数来分配特征。这意味着,即使你将白色面孔的权重定义为60,你也不可能正好有60张白色面孔。为了准确了解每个特征的出现数量,必须跟踪现在有多少特征出现在你的图像集合中。
要做到这一点,要写下面的代码。
- 为每个特征定义一个字典,其中有它们各自的分类,并从0开始。
循环查看你创建的图像,如果遇到特质,就把它们添加到各自的特质字典中。
1. `# Get Trait Counts`
2.
3. `face_count = {}`
4. `for item in face:`
5. `face_count[item] = 0`
6.
7. `ears_count = {}`
8. `for item in ears:`
9. `ears_count[item] = 0`
10.
11. `eyes_count = {}`
12. `for item in eyes:`
13. `eyes_count[item] = 0`
14.
15. `hair_count = {}`
16. `for item in hair:`
17. `hair_count[item] = 0`
18.
19. `mouth_count = {}`
20. `for item in mouth:`
21. `mouth_count[item] = 0`
22.
23. `nose_count = {}`
24. `for item in nose:`
25. `nose_count[item] = 0`
26.
27. `for image in all_images:`
28. `face_count[image["Face"]] += 1`
29. `ears_count[image["Ears"]] += 1`
30. `eyes_count[image["Eyes"]] += 1`
31. `hair_count[image["Hair"]] += 1`
32. `mouth_count[image["Mouth"]] += 1`
33. `nose_count[image["Nose"]] += 1`
34.
35. `print(face_count)`
36. `print(ears_count)`
37. `print(eyes_count)`
38. `print(hair_count)`
39. `print(mouth_count)`
40. `print(nose_count)`
生成图像
这是最神奇的部分。对于每张图片,脚本将执行以下操作。
- 打开我们定义特质的图像特征文件
- 使用PIL软件包在你的目录中选择相应的性状图像。
- 将所有的性状组合成一个图像
- 转换为RGB,这是最传统的颜色模型
- 把它保存到你的电脑上
1. `#### Generate Images`
2.
3. `os.mkdir(f'./images')`
4.
5. `for item in all_images:`
6.
7. `im1 = Image.open(f'./scripts/face_parts/face/{face_files[item["Face"]]}.png').convert('RGBA')`
8. `im2 = Image.open(f'./scripts/face_parts/eyes/{eyes_files[item["Eyes"]]}.png').convert('RGBA')`
9. `im3 = Image.open(f'./scripts/face_parts/ears/{ears_files[item["Ears"]]}.png').convert('RGBA')`
10. `im4 = Image.open(f'./scripts/face_parts/hair/{hair_files[item["Hair"]]}.png').convert('RGBA')`
11. `im5 = Image.open(f'./scripts/face_parts/mouth/{mouth_files[item["Mouth"]]}.png').convert('RGBA')`
12. `im6 = Image.open(f'./scripts/face_parts/nose/{nose_files[item["Nose"]]}.png').convert('RGBA')`
13.
14. `#Create each composite`
15. `com1 = Image.alpha_composite(im1, im2)`
16. `com2 = Image.alpha_composite(com1, im3)`
17. `com3 = Image.alpha_composite(com2, im4)`
18. `com4 = Image.alpha_composite(com3, im5)`
19. `com5 = Image.alpha_composite(com4, im6)`
20.
21.
22.
23. `#Convert to RGB`
24. `rgb_im = com5.convert('RGB')`
25. `file_name = str(item["tokenId"]) + ".png"`
26. `rgb_im.save("./images/" + file_name)`
- 点击下方阅读原文加入
社区会员 -