当前位置:网站首页>Creating ASCII art with C #
Creating ASCII art with C #
2022-07-01 01:31:00 【woshicver】

You've seen those cool “ Letter ” Art ? Using some English letters and symbols to form some kind of text or image ? Some people try to create this art letter by letter , But today we will do it ourselves !
What is? ASCII art ?
Basically ,ASCII Art is a text-based art form . Artistic text or text used in programs or documents , From ASCII The characters and symbols of characters .
What is? ASCII:https://en.wikipedia.org/wiki/ASCII
How it works ?
Good question !
To explain this , We must understand what the image is made of .
Images , More specifically , A digital image consists of pixels , Based on the resolution of the image .
therefore , A resolution of 400 * 600( high \ wide ) The image of 240000 Pixels . Pixels are the smallest part of a digital image . Each pixel can contain data about the color of that pixel , Pixels have different numbers of bits to represent them , Each bit represents a color option
1 bpp,2^1 = 2 Color ( monochrome )
2 bpp,2^2 = 4 Color
3 bpp,2^3 = 8 Color
4 bpp, 2^4 = 16 color
8 bpp,2^8 = 256 color
16 bpp, 2^16 = 65,536 color (“ High color ”)
24 bpp, 2^24 = 16,777,216 color (“ True color ”)
High color :https://en.wikipedia.org/wiki/Highcolor
True color :https://en.wikipedia.org/wiki/24-bit_color
So how do we construct colors ?
There are several ways to define colors using color space , One of the most popular is sRGB.
sRGB Represents standard red, green and blue , It's a color space , Or a specific set of colors , By HP and Microsoft on 1996 Created in , The goal is to standardize the colors depicted by electronic products .
let me put it another way , Red 、 The combination of green and blue will produce different colors . The range of each value can be in 0 To 255 Between , So take the color representation as an example :
(R:255,G:0,B:0) = Red
(R:0,G:255,B:0) = green
(R: 0,G:0,B:255) = Blue
(R:255,G:255,B:255) = white
(R:0,G:0,B:0) = black
(R:100,G:100 ,B:100) = gray
Let's move on to code implementation .
Print the image as ASCII art
hypothesis , We want to convert this image to ASCII art :

Lovely little dog
Please note that , We have some parts of the picture that are darker , Some parts are brighter . This is exactly what we are creating ASCII What you want to simulate in Art , Some characters can represent a dark pixel , Such as spaces “” Or point “.”. Some can represent a bright pixel , for example “@” or “W”, But why should I choose these ? It's easy to understand , The denser the letters , The brighter it will look on a dark background, right ?
So let's define our “ palette ”:
string asciiChars = " .,:[email protected]@";These letters must be sorted according to their density , To work properly .

Suppose we have a pixel , And we know it is RGB value , How do we translate it into ASCII character , If we look at its grayscale color , It will be easier to understand the brightness or darkness of pixels . The grayscale hue can be determined by RGB The average of the values generates .
for example , Red (255,0,0) Gray scale is expressed as (85,85,85).
Start with this part , All that's left is to take the average and get its position in the character array we created earlier .
therefore , If the color average has [0,255] Maybe , and ASCII Characters only [0,10] Maybe , We want to be in the ASCII Take the relative part of the color value in the character array ,
The more letters we put in the character array , We will have more possibilities to represent our brightness value .

I don't want to show every pixel , Because there are too many pixels . So I resized the image to match 130 The width of the pixel .
final result :

This is great. !
But I still need to change the font , Make the space between letters even , And fine tune the row height , So that the picture is displayed in the correct size , Instead of looking crushed .
Does it also apply to video ?
And then I thought , Video is basically a collection of digital images ! If I change my code , From a short mp4 How about extracting frames from video files ?
It's possible , I just need to find a way to extract frames , And reuse my code . So I found one named NuGet Gallery | Emgu.CV Cross platform of image processing library .NET Wrappers .
NuGet Gallery | Emgu.CV :https://www.nuget.org/packages/Emgu.CV/
To reduce console write flicker , I collected every character that needs to be inserted into the string builder object , And when processing the pixels of each frame is completed , I will have to StringBuilder Values are pushed to the console . And position the console cursor at the beginning of the text

Look at this. :

Isn't it cool !
The last step
The last step , Can we parse the video in real time ?
Use the same library , You can easily set video capture as a webcam , And see yourself as a bunch “W” and “@” and “.”

Happy learning !
* END *
If you see this , Show that you like this article , Please forward 、 give the thumbs-up . WeChat search 「uncle_pn」, Welcome to add Xiaobian wechat 「 woshicver」, Update a high-quality blog post in your circle of friends every day .
↓ Scan QR code and add small code ↓

边栏推荐
- Note d'étude du DC: zéro dans le chapitre officiel - - Aperçu et introduction du processus de base
- 医疗HIS行业短信发送解决方案
- Install redis database and download redis Desktop Manager in win11
- Pytorch programming knowledge (2)
- Dls-20 double position relay 220VDC
- DC学习笔记正式篇之零——综述与基本流程介绍
- 那些一门心思研究自动化测试的人,后来怎样了?
- 【qt5-tab标签精讲】Tab标签及内容分层解析
- Construction and beautification of personal blog
- Open3d point cloud bounding box
猜你喜欢

K210 site helmet

Basic knowledge of software and hardware -- diary (1)

微研所,微生物检验中常用的生化反应

Docker deployment MySQL 8
![Parity linked list [two general directions of linked list operation]](/img/4e/ce860bc172bb75f456427ba26a7842.png)
Parity linked list [two general directions of linked list operation]

迪赛智慧数——其他图表(平行坐标图):2021年应届专业就业情况

Using recyclerreview to show banner is very simple

Zero of DC learning notes -- overview and basic process introduction
![[network packet loss and network delay? This artifact can help you deal with everything!]](/img/c4/f733b23327458b9266b9cbcccb6f14.png)
[network packet loss and network delay? This artifact can help you deal with everything!]

Use of typora
随机推荐
MFC TCP communication server client demo notes vs2019
[network packet loss and network delay? This artifact can help you deal with everything!]
mysql数据库基础:流程控制
为什么要搭建个人博客
基础知识之一——STA基础概述
Install redis database and download redis Desktop Manager in win11
Sécurité et santé microbiennes, qu'est - ce que le traitement biologique?
Using recyclerreview to show banner is very simple
Open3D 点云包围盒
短信在企业中的应用有哪些?
[learning notes] structure
DC學習筆記正式篇之零——綜述與基本流程介紹
1500w播放下还藏着什么热点?B站2个未来趋势你不得错过
Visual studio 2019 shortcut notes
Split the linked list [take next first and then cut the linked list to prevent chain breakage]
Service grid ASM year end summary: how do end users use the service grid?
What are the functions of soil microorganisms in microbial detection?
visual studio 2019 快捷键备忘
Introduction and principle analysis of cluster and LVS
6月第4周榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!