字符畫,一種由字母、標(biāo)點(diǎn)、漢字或其他字符組成的圖畫。簡(jiǎn)單的字符畫是利用字符的形狀代替圖畫的線條來構(gòu)成簡(jiǎn)單的人物、事物等形象,它一般由人工制作而成;復(fù)雜的字符畫通常利用占用不同數(shù)量像素的字符代替圖畫上不同明暗的點(diǎn),它一般由程序制作而成。字符畫是互聯(lián)網(wǎng)時(shí)代的產(chǎn)物,通常應(yīng)用于即時(shí)聊天中。
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))) # 調(diào)整圖片大小
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')
到此這篇關(guān)于Python制作動(dòng)態(tài)字符畫的源碼的文章就介紹到這了,更多相關(guān)Python動(dòng)態(tài)字符畫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!