当前位置:网站首页>情人节,我用字符画出了一个对象!
情人节,我用字符画出了一个对象!
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库把原视频的声音导入过来。
完整代码
边栏推荐
- IBM3650M4实体机安装VCenter7.0
- "PHP Basics" uses echo statements to output information
- Data extraction 1
- mqtt指令收发请求订阅
- [netding cup 2020 Qinglong group]areuserialz (buuctf)
- [applet] how to get wechat applet code upload key?
- Is redis really slowing down?
- An ordinary autumn recruitment experience
- Dirsearch[directory scanning tool]
- Leetcode54. Spiral matrix
猜你喜欢

Use of string type "PHP Basics"

Is redis really slowing down?
![[target detection] yolov6 theoretical interpretation + practical test visdrone data set](/img/ad/78835eea4decc15e0981e6561b875f.png)
[target detection] yolov6 theoretical interpretation + practical test visdrone data set

Abstract factory pattern

C event usage case subscription event+=
![[applet] the upload of the wechat applet issued by uniapp failed error: error: {'errcode': -10008,'errmsg':'Invalid IP](/img/0c/da2ffc00834793c7abc0f7ebe6321b.png)
[applet] the upload of the wechat applet issued by uniapp failed error: error: {'errcode': -10008,'errmsg':'Invalid IP

一段平平无奇的秋招经历

Solve the problem of slow batch insertion of MySQL JDBC data

如何获取广告服务流量变现数据,助力广告效果分析?

Use of "PHP Basics" delimiters
随机推荐
一段平平无奇的秋招经历
JS access cookie example
Harbor正确密码登录不上去
Use of string type "PHP Basics"
[ten thousand words long article] thoroughly understand load balancing, and have a technical interview with Alibaba Daniel
A quick overview of transformer quantitative papers in emnlp 2020
大家节日快乐哈
My senior
浅谈数据安全
Digital transformation driven by enterprise architecture!
Sword finger offer 58 - I. flip word order
Stored procedure test 1 -- first acquaintance of love
Happy holidays, everyone
Things come to conform, the future is not welcome, at that time is not miscellaneous, neither love
mqtt指令收发请求订阅
Kalibr calibration realsensed435i -- multi camera calibration
I can't figure out why MySQL uses b+ trees for indexing?
The response of the database interface is very slow
Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click
npm的使用