python游戏开发之视频转彩色字符动画
作者:狡猾的皮球 时间:2022-05-18 21:11:23
本文实例为大家分享了python视频转彩色字符动画的具体代码,供大家参考,具体内容如下
一、效果
原图:
转换后:
效果可通过代码开头几行的参数调节
二、代码
开头几行代码,自己看着调整,把效果调到最佳就ok。
依赖库:
pip install opencv-python
pip install pygame
代码:
import pygame
import cv2
FONT_SIZE = 18 # 字体大小,可自行调整
WIN_SIZE = (1440, 1000) # 窗口大小,可自行调整
VIDEO_SIZE = (30, 30) # 视频大小,可自行调整
VIDEO_PATH = './cat.gif' # 视频文件(可以为常见的视频格式和gif)
STR_TEXT = '假装失智' # 替换字符,可自定义,没有长度限制,但至少得有一个
def video2imgs(video_name, size):
img_list = []
cap = cv2.VideoCapture(video_name)
while cap.isOpened():
ret, frame = cap.read()
if ret:
img = cv2.resize(frame, size, interpolation=cv2.INTER_AREA)
img_list.append(img)
else:
break
cap.release()
return img_list
# 初始化pygame
def main():
pygame.init()
winSur = pygame.display.set_mode(WIN_SIZE)
imgs = video2imgs(VIDEO_PATH, VIDEO_SIZE)
btnFont = pygame.font.SysFont("fangsong", FONT_SIZE)
btnFont.set_bold(True)
# 生成surface
sur_list = []
for img in imgs:
height, width, color = img.shape
surface = pygame.Surface(WIN_SIZE)
a = 0
x, y = 0, 0
for row in range(height):
x = 0
for col in range(width):
# 获取当前像素RGB
rgb = img[row][col]
rgb[0], rgb[2] = rgb[2], rgb[0]
text_texture = btnFont.render(STR_TEXT[a], True, rgb)
a = a + 1
a = a % len(STR_TEXT)
surface.blit(text_texture, (x, y))
x = x + FONT_SIZE
y = y + FONT_SIZE
sur_list.append(surface)
# 游戏主循环
current_frame = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.time.delay(int(1000 / 24))
winSur.fill((0, 0, 0))
winSur.blit(sur_list[current_frame], [0, 0])
current_frame += 1
current_frame %= len(sur_list)
# 刷新界面
pygame.display.flip()
if __name__ == '__main__':
main()
来源:https://blog.csdn.net/qq_39687901/article/details/83347915
标签:python,视频,字符动画
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何实现SQL Server 2005快速Web分页
2009-01-21 14:51:00
python框架Django实战商城项目之工程搭建过程图文详解
2022-12-16 16:25:57
![](https://img.aspxhome.com/file/2023/6/72046_0s.jpg)
用CSS3和HTML5五步打造便签效果
2012-04-25 20:47:51
Python游戏推箱子的实现
2023-09-23 05:31:27
![](https://img.aspxhome.com/file/2023/9/70759_0s.jpg)
在查询分析器理启动或停止SQL Agent服务
2009-01-08 16:20:00
PHP 二维数组根据某个字段排序的具体实现
2023-11-20 05:21:13
浏览器中的内存泄露
2008-05-03 16:53:00
![](https://img.aspxhome.com/file/UploadPic/20085/3/200853171337633s.gif)
X/HTML5 v.s. XHTML2(II)
2008-06-18 13:19:00
SQL Server数据库涉及到的数据仓库概念
2009-01-15 12:58:00
Python实现运行其他程序的四种方式实例分析
2023-08-03 00:48:24
![](https://img.aspxhome.com/file/2023/1/61741_0s.png)
Python查询缺失值的4种方法总结
2023-10-29 13:42:08
![](https://img.aspxhome.com/file/2023/1/90871_0s.png)
python实现双链表
2022-06-20 01:47:48
![](https://img.aspxhome.com/file/2023/4/92154_0s.jpg)
Pytorch自定义CNN网络实现猫狗分类详解过程
2023-10-27 19:51:02
![](https://img.aspxhome.com/file/2023/3/72083_0s.png)
设置jupyter中DataFrame的显示限制方式
2022-09-17 17:28:57
python右对齐的实例方法
2022-01-15 20:59:52
详解Python中的format格式化函数的使用方法
2023-04-12 22:47:05
![](https://img.aspxhome.com/file/2023/2/77612_0s.png)
FrontPage XP设计制作网页小技巧八则
2008-06-04 12:43:00
可能被你忽略的 JavaScript 代码陷阱
2009-12-26 18:16:00
Win10下python3.5和python2.7环境变量配置教程
2021-08-31 10:40:23
![](https://img.aspxhome.com/file/2023/3/80623_0s.png)
如何创建并使用一个断开连接的记录集的数据访问页?
2009-11-14 20:50:00