当前位置:网站首页>情人节,我用字符画出了一个对象!
情人节,我用字符画出了一个对象!
2022-07-27 08:12:00 【51CTO】
之前经常在网上看到那种由一个个字符构成的视频,非常炫酷。一直不懂是怎么做的,这两天研究了一下,发现并不难。
先来看一个最终效果(如果模糊的话,点击下方链接看高清版):
https://bytedance.feishu.cn/docx/doxcnCvvY051xFCBAmSqYZgiP7b
怎么实现的?
简单来说,要将一个彩色的视频变成字符画出来的黑白视频,用下面几步就能搞定:
- 对原视频进行抽帧,对每一帧黑白化,并将像素点用对应的字符表示。
- 将表示出来的字符串再重新组合成字符图像。
- 将所有的字符图像再组合成字符视频。
- 将原视频的音频导入到新的字符视频中。
运行方法
完整的代码我放在文章末尾了,直接运行python3 video2char.py即可。程序会要求你输入视频的本地路径和转变后的清晰度(0最模糊,1最清晰。当然越清晰,转变越慢)。
运行代码的话需要用到tqdm、opencv_python、moviepy等几个库,首先得pip3 install确保它们都有了。
原理分析
这里面最关键的步骤就是如何将一帧彩色图像转变为黑白的字符图像,如下图所示:

从青蛙公主视频抽帧出来的

用字符画出来的
而转变的原理其实很简单。首先因为一个字符画在图像里会占据很大一个像素块,所以必须先对彩色图像进行压缩,连续的一个像素块可以合并,这个压缩过程就是opencv的resize操作。
然后将压缩后的像素点转变为黑白像素点,并转变为对应的字符。字符的话我这里采用的是下面的字符串,从黑到白,经过我的实践这一组是效果最好的:
"#8XOHLTI)i=+;:,. "
接着就需要将转变后的字符画到新的画布上去,需要注意的点是排布得均匀紧凑了,画布四周最好不要有太多多余的空白。
最后把所有的字符图像合并成视频就行了,但是合并后是没有声音的,需要用moviepy库把原视频的声音导入过来。
完整代码
边栏推荐
- End of year summary
- "PHP Basics" uses echo statements to output information
- "PHP Basics" use of integer data
- You may need an additional loader to handle the result of these loaders.
- 如何在 60 秒内去分析和定位问题?
- Digital transformation driven by enterprise architecture!
- [golang] golang develops wechat official account web page authorization function
- 物联网工业级UART串口转WiFi转有线网口转以太网网关WiFi模块选型
- Lua迭代器
- 孙子出题难,儿子监考严。老子不会做,还我上学钱
猜你喜欢

Ubuntu: install PostgreSQL

pytorch_demo1

Record a PG master-slave setup and data synchronization performance test process
![[applet] how to get wechat applet code upload key?](/img/b4/76e2f12269601c0a969a709ff8a397.png)
[applet] how to get wechat applet code upload key?

1024 | in the fourth year officially called Menon, the original intention is still there, and continue to move forward

Use of "PHP Basics" Boolean

"Intermediate and advanced test questions": what is the implementation principle of mvcc?

"PHP Basics" uses echo statements to output information

物联网工业级UART串口转WiFi转有线网口转以太网网关WiFi模块选型

抽象工厂模式
随机推荐
杂谈:把肉都烂在锅里就是保障学生权益了?
You may need an additional loader to handle the result of these loaders.
Lua stateful iterator
[applet] how to get wechat applet code upload key?
Design and development of GUI programming for fixed-point one click query
docker 安装mysql后进入容器内部发现登录不了mysql
瑞芯微RK3399-I2C4挂载EEPROM的修改案例
1024 | in the fourth year officially called Menon, the original intention is still there, and continue to move forward
[applet] the upload of the wechat applet issued by uniapp failed error: error: {'errcode': -10008,'errmsg':'Invalid IP
redis配置文件下载
Day111. Shangyitong: integrate nuxt framework, front page data, hospital details page
The seta 2020 international academic conference will be held soon. Welcome to attend!
大家节日快乐哈
Opengauss stopped from the library and found that the main library could not write data
Stored procedures and functions
C language: random generated number + insertion sort
Data extraction 2
"PHP Basics" tags in PHP
idea远程调试
Download and usage of sequel Pro