不知道大家喜不喜歡玩字符畫
有什麼python相關報錯解答自己不會的、或者源碼資料/模塊安裝/
女裝大佬精通技巧都可以來這裡:(https://jq.qq.com/?_wv=1027&k=2Q3YTfym)或者+V:python10010問我
前一陣子本熊貓我看見了一個很牛的字符畫,當時就震驚了,不就是個字符畫~我覺得自己也行
然後我就開始了簡單版的嘗試
這還不是輕輕松松有手就行 ~ ~ ~
from PIL import Image as im
from tkinter import *
import cv2
# 隨便打
codeLib = '''*.1'''
count = len(codeLib)
def transform(image_file):
codePic = ''
for h in range(0, image_file.size[1]):
for w in range(0, image_file.size[0]):
g, r, b = image_file.getpixel((w, h))
gray = int(r * 0.299 + g * 0.587 + b * 0.114)
codePic = codePic + codeLib[int(((count - 1) * gray) / 256)]
codePic = codePic + '\r\n'
return codePic
def image2char(image_file):
image_file = image_file.resize((int(image_file.size[0] * 0.16), int(image_file.size[1] * 0.06))) # 調整圖片大小
return transform(image_file), image_file.size[0], image_file.size[1]
def frame2image(cap, i):
cap.set(cv2.CAP_PROP_POS_FRAMES, i)
_, b = cap.read()
image = im.fromarray(cv2.cvtColor(b, cv2.COLOR_BGR2RGB))
return image
def gui(path):
cap = cv2.VideoCapture(path)
root = Tk()
t = frame2image(cap, 0)
_, w, h = image2char(t)
text = Text(root, width=w, height=h)
text.pack()
framenum = int(cap.get(7))
for i in range(framenum):
image = frame2image(cap, i)
content, _, _ = image2char(image)
text.insert(INSERT, content)
root.update()
text.delete(0.0, END)
if __name__ == '__main__':
gui(r'C:\Users\Administrator\Desktop\油性極大.mp4')
素材用的是…
最後選擇視頻的路徑,修改代碼的文件路徑,在運行代碼