脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python制作动态字符画的源码

Python制作动态字符画的源码

2021-12-16 10:34松鼠爱吃饼干 Python

python字符画是一个简单有趣的图画,它一般由程序制作而成,接下来通过本文给大家分享Python制作动态字符画的源码,需要的朋友可以参考下

字符画,一种由字母、标点、汉字或其他字符组成的图画。简单的字符画是利用字符的形状代替图画的线条来构成简单的人物、事物等形象,它一般由人工制作而成;复杂的字符画通常利用占用不同数量像素的字符代替图画上不同明暗的点,它一般由程序制作而成。字符画是互联网时代的产物,通常应用于即时聊天中。

首先,也是最重要的,先放源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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')

然后,选择一个短视频

Python制作动态字符画的源码

最后选择视频的路径,修改代码的文件路径,在运行代码

Python制作动态字符画的源码

到此这篇关于python制作动态字符画的源码的文章就介绍到这了,更多相关python动态字符画内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/qshhl/p/15084578.html

延伸 · 阅读

精彩推荐