当前位置:网站首页>情人节,我用字符画出了一个对象!
情人节,我用字符画出了一个对象!
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库把原视频的声音导入过来。
完整代码
边栏推荐
- C commissioned use cases
- My senior
- [day42 literature intensive reading] a Bayesian model of perfect head centered velocity during smooth pursuit eye movement
- [netding cup 2020 Qinglong group]areuserialz (buuctf)
- [target detection] yolov6 theoretical interpretation + practical test visdrone data set
- What is the real HTAP? (1) Background article
- Methods of server network testing
- 物来顺应,未来不迎,当时不杂,既过不恋
- 2020国际机器翻译大赛:火山翻译力夺五项冠军
- [resolved] the new version of pychart (2022) connects to the server to upload files and reports an error of "command Rsync is not found in path", and the files cannot be synchronized
猜你喜欢

An ordinary autumn recruitment experience

Qt Creator代码风格插件Beautifier

Abstract factory pattern

2020国际机器翻译大赛:火山翻译力夺五项冠军

一文速览EMNLP 2020中的Transformer量化论文

Grandson's questions are difficult, and his son's invigilation is strict. I can't do it. Pay back my school money

Leetcode54. Spiral matrix

QT creator code style plug-in beautifier

2020 International Machine Translation Competition: Volcano translation won five championships

北京五日游记
随机推荐
vCenter7.0管理Esxi7.0主机
Promise详解
You may need an additional loader to handle the result of these loaders.
How does slf4j configure logback?
Virtual machine cloning
Modification case of Ruixin micro rk3399-i2c4 mounting EEPROM
Use of NPM
Enhancement: BTE process introduction
Stored procedure trial 2 -- establish a test table to test different types of stored procedures
Kalibr calibration realsensed435i -- multi camera calibration
"PHP Basics" uses echo statements to output information
Risk control and application of informatization project
[netding cup 2020 Qinglong group]areuserialz (buuctf)
Data extraction 1
【Day42 文献精读】A Bayesian Model of Perceived Head-Centered Velocity during Smooth Pursuit Eye Movement
What is a rebound shell? What's the use of bouncing shells?
Shell Scripts相关
Day111. Shangyitong: integrate nuxt framework, front page data, hospital details page
如何更新pip3?和Running pip as the ‘root‘ user can result in broken permissions and conflicting behaviour
Combined use of C WinForm form form event and delegate