---
# System prepended metadata

title: 做出自己的簡易nft
tags: [自己玩]

---

# 做出自己的簡易nft
###### tags: `自己玩`
## 眼睛
```python=

#將圖片變成我要的大小 但圖片不便大小 底色為黑色
import cv2
import os
import numpy as np
os.chdir('C:/Users/bt/eyee/')
def img_resize_to_target_black(image):
    target = np.zeros((600,600),dtype=np.uint8)

    bgr_img = cv2.cvtColor(target, cv2.COLOR_GRAY2BGR)
    
    h = image.shape[0]
    w = image.shape[1]
    for i in  range(600):
        for j in  range(600):
            if(i >= h) and (j >= w):
                
                bgr_img[i, j, 0] = 204
                bgr_img[i, j, 1] = 224
                bgr_img[i, j, 2] = 255
            if (i >= h) and (j < w):
                bgr_img[i, j, 0] = 204
                bgr_img[i, j, 1] = 224
                bgr_img[i, j, 2] = 255
            if(i < h) and (j < w) :
                bgr_img[i, j, 0] = image[i, j, 0]
                bgr_img[i, j, 1] = image[i, j, 1]
                bgr_img[i, j, 2] = image[i, j, 2]
                bgr_img[i, j+290, 0] = image[i, -j, 0]
                bgr_img[i, j+290, 1] = image[i, -j, 1]
                bgr_img[i, j+290, 2] = image[i, -j, 2]

    return bgr_img

p = 'C:/Users/bt/eye/'
p = os.listdir(p)
for i in p:
    if __name__ == '__main__':
        image = cv2.imread('C:/Users/bt/eye/'+i)
        image = cv2.flip(image,1)
        img_new_black = img_resize_to_target_black(image)
        cv2.imwrite(str(i), img_new_black)

```
在eye資料夾
![](https://i.imgur.com/hieihNX.png)
在eyee資料夾
![](https://i.imgur.com/Hd83PNR.png)
## 嘴巴
```python=
#將圖片變成我要的大小 但圖片不便大小 底色為黑色
import cv2
import os
import numpy as np
os.chdir('C:/Users/bt/mouthe/')
def img_resize_to_target_black(image):
    target = np.zeros((600,600),dtype=np.uint8)

    bgr_img = cv2.cvtColor(target, cv2.COLOR_GRAY2BGR)
    
    h = image.shape[0]
    w = image.shape[1]
    for i in  range(600):
        for j in  range(600):
           
            if (i not in range(400,600)):
                bgr_img[i, j, 0] = 204
                bgr_img[i, j, 1] = 224
                bgr_img[i, j, 2] = 255
               
            if i in range(400,600) and j in range(170):
                bgr_img[i, j, 0] = 204
                bgr_img[i, j, 1] = 224
                bgr_img[i, j, 2] = 255
            if i in range(400,600) and j in range(440,600):
                bgr_img[i, j, 0] = 204
                bgr_img[i, j, 1] = 224
                bgr_img[i, j, 2] = 255
            if (i in range(580,600)):
                bgr_img[i, j, 0] = 204
                bgr_img[i, j, 1] = 224
                bgr_img[i, j, 2] = 255
            if(i < h) and (j < w) :
                bgr_img[i+400, j+170, 0] = image[i, j, 0]
                bgr_img[i+400, j+170, 1] = image[i, j, 1]
                bgr_img[i+400, j+170, 2] = image[i, j, 2]
            
            

    return bgr_img

p = 'C:/Users/bt/mouth/'
p = os.listdir(p)
for i in p:
    if __name__ == '__main__':
        image = cv2.imread('C:/Users/bt/mouth/'+i)
        image = cv2.flip(image,1)
        img_new_black = img_resize_to_target_black(image)
        cv2.imwrite(str(i), img_new_black)

```
![](https://i.imgur.com/nU3qki6.png)

![](https://i.imgur.com/mNLhX3B.png)



## 鼻子
```python=
import cv2
import os
import numpy as np
os.chdir('C:/Users/bt/nosee/')
def img_resize_to_target_black(image):
    target = np.zeros((600,600),dtype=np.uint8)

    bgr_img = cv2.cvtColor(target, cv2.COLOR_GRAY2BGR)
    
    h = image.shape[0]
    w = image.shape[1]
    for i in  range(600):
        for j in  range(600):
           
            if ((i not in range(220,380))and j not in range(220,380)):
                bgr_img[i, j, 0] = 204
                bgr_img[i, j, 1] = 224
                bgr_img[i, j, 2] = 255
               
            if ((i  in range(220,600))and j not in range(220,380)):
                bgr_img[i, j, 0] = 204
                bgr_img[i, j, 1] = 224
                bgr_img[i, j, 2] = 255
            if ((i not in range(220,380))and j  in range(220,600)):
                bgr_img[i, j, 0] = 204
                bgr_img[i, j, 1] = 224
                bgr_img[i, j, 2] = 255
            if(i < h) and (j < w) :
                bgr_img[i+200, j+170, 0] = image[i, j, 0]
                bgr_img[i+200, j+170, 1] = image[i, j, 1]
                bgr_img[i+200, j+170, 2] = image[i, j, 2]
            
            

    return bgr_img

p = 'C:/Users/bt/nose/'
p = os.listdir(p)
for i in p:
    if __name__ == '__main__':
        image = cv2.imread('C:/Users/bt/nose/'+i)
        image = cv2.flip(image,1)
        img_new_black = img_resize_to_target_black(image)
        cv2.imwrite(str(i), img_new_black)
```
![](https://i.imgur.com/endCZId.png)

![](https://i.imgur.com/XtoP22J.png)

## 合成各種不同照片
```python=
import random
from PIL import Image 
import cv2 
import os



#讀取資料以及設置權重
os.chdir('C:/Users/bt/hahahaha')

p = 'C:/Users/bt/nosee'
nosee = os.listdir(p)
nosee_weight = []
for i in range(len(nosee)):
    nosee_weight.append(100/len(nosee))

p = 'C:/Users/bt/eyee'
eyee = os.listdir(p)
eyee_weight = []
for i in range(len(eyee)):
    eyee_weight.append(100/len(eyee))

p = 'C:/Users/bt/mouthe'
mouthe = os.listdir(p)

mouthe_weight = []
for i in range(len(mouthe)):
    mouthe_weight.append(100/len(mouthe))

    
    
    
TOTAL_IMAGES = 10#要做幾次
all_images = [] 

#隨機抽取個個圖片並駔成一組
def create():
    new_image = {}
    
    
    new_image ["nosee"] = random.choices(nosee, nosee_weight)[0]
    new_image ["eyee"] = random.choices(eyee, eyee_weight)[0]
    new_image ["mouthe"] = random.choices(mouthe, mouthe_weight)[0]
    if new_image in all_images:
        return create()
    else:
        return new_image

for i in range(TOTAL_IMAGES): 
    
    new_trait_image = create()
    
    all_images.append(new_trait_image)
    
for i in range(TOTAL_IMAGES): 
    
    new_trait_image = create()
    
    all_images.append(new_trait_image)

判斷是否都不一樣
def all_images_unique(all_images):
    seen = list()
    return not any(i in seen or seen.append(i) for i in all_images)

print("Are all images unique?", all_images_unique(all_images))

#每組加編號
i = 0
for item in all_images:
    item["tokenId"] = i
    i = i + 1
   
print(all_images)



nosee_count = {}
for item in nosee:
    nosee_count[item] = 0
    
eyee_count = {}
for item in eyee:
    eyee_count[item] = 0
    
mouthe_count = {}
for item in mouthe:
    mouthe_count[item] = 0
    
for image in all_images:
    nosee_count[image["nosee"]] += 1
    eyee_count[image["eyee"]] += 1
    mouthe_count[image["mouthe"]] += 1
    
print(nosee_count)
print(eyee_count)
print(mouthe_count)

i = 0
for item in all_images:
    item["tokenId"] = i
    i = i + 1
    
for i in all_images:
    
    im1 = Image.open(f'C:/Users/bt/nosee/'+i['nosee']).convert('RGBA')
    im2 = Image.open(f'C:/Users/bt/eyee/'+i['eyee']).convert('RGBA')
    im3 = Image.open(f'C:/Users/bt/mouthe/'+i['mouthe']).convert('RGBA')
    
    im1 = im1.resize((150, 150), Image.ANTIALIAS)
    im2 = im2.resize((150,150), Image.ANTIALIAS)
    im3 = im3.resize((150,150), Image.ANTIALIAS)
    
    #將除了圖片其他地方變透明
    x, y = im2.size
    for p in range(x):
        for k in range(y):
            color = im2.getpixel((p, k))
            color = color[:-1] + (100, )
            im2.putpixel((p, k), color)
    x, y = im3.size
    for p in range(x):
        for k in range(y):
            color = im3.getpixel((p, k))
            color = color[:-1] + (100, )
            im3.putpixel((p, k), color)   
  
    #合成
    com1 = Image.alpha_composite(im1, im2)
    com2 = Image.alpha_composite(com1,im3)
    
    rgb_im = com2.convert('RGB')
    file_name = str(i["tokenId"]) + ".png"
   
    rgb_im.save(file_name)
```
![](https://i.imgur.com/I71ZEOi.png)
## 自製加密方法
want 、arc為圖的編號 , c為密鑰需要緊記
```python=
import random
want = input("請輸入你要的圖號碼:")
a = ['d1s','qeg','25e','k6g','w4g','efr']
b = ['wd','fk4o','sd3ko','s9ko']
c = []
num = ['1','2','3','4','5','6','7','8','9','0']
for i in a:
    c.append(random.choice(a))
c.append(all_images['tokenId'][want])
for i in b:
    c.append(random.choice(b))

c = list(map(str,c))
ans = ''
c =''.join(c)
ok = 1
l = 1

while ok == 1:
    if c[17+l] in num:
        l += 1
       
    if c[17+l] not in num:
        anc = c[18:17+l]
        
        ok = 0
```
## 製作平台
做出2個輸入格,1個為製造出密匙,1個為輸入密鑰匙並顯示圖案
```python=
from tkinter import * 
from PIL import Image ,ImageTk
import tkinter.messagebox
import random
root = Tk()#建立一個視窗實體
root.title('ha')#視窗名子
root.geometry('500x150')#視窗大小


text = Text(root, font = ('ariel',10))#字體大小
def produce_key_number():
    global result
    result = current_input.get()
    a = ['d1s','qeg','25e','k6g','w4g','efr']
    b = ['wd','fk4o','sd3ko','s9ko']
    c = []
    num = ['1','2','3','4','5','6','7','8','9','0']
    for i in a:
        c.append(random.choice(a))
    c.append(str(result))
    for i in b:
        c.append(random.choice(b))

    c = list(map(str,c))
    c =''.join(c)
    msg = "your key number is : {}.(please remember it)".format(str(c))
    tkinter.messagebox.showinfo(title = 'key number', # 視窗標題
                                message = msg,default = 'ok')
    
def search():
    
    top = Toplevel()
    c = str(current2_input.get())
    num = ['1','2','3','4','5','6','7','8','9','0']
    ok = 1
    l = 1
    while ok == 1:
        if c[17+l] in num:
            l += 1
        if c[17+l] not in num:
            anc = c[18:17+l]
            ok = 0
    img= ImageTk.PhotoImage(Image.open('C:/Users/bt/hahahaha/'+anc+'.png'))
    label_right= Label(top,height=460,width=480,bg ='gray94',fg='blue',image = img) 
    label_right.pack(side = TOP, expand = True, fill = BOTH) 
    
    root.mainloop()

    
    
label = Label(root,text = "Enter the picture's number you want:",wraplength = 250)  # 顯示文字

label.pack()

current_input = Entry(root, width = 30)#搜尋欄
current_input.pack(pady=10)#pack為位置 空的代表預設

button = Button(root,text = 'enter',command = produce_key_number)
button.pack()

    
label1 = Label(root,text = "Enter the key_number you have:",wraplength = 250)  # 顯示文字

label1.pack()

current2_input = Entry(root, width = 30)#搜尋欄
current2_input.pack()#pack為位置 空的代表預設





button2 = Button(root,text = "see your's picture",command = search)
button2.pack()



text.pack()

root.mainloop()

```
生成密鑰
![](https://i.imgur.com/hsJYHmW.png)
查看得到的圖
![](https://i.imgur.com/ydc4AvK.png)





