当前位置:网站首页>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 ↓

边栏推荐
- 1500w播放下还藏着什么热点?B站2个未来趋势你不得错过
- WIN11中MathType编辑中“打开数学输入面板”是灰色不可编辑
- 1175. Prime Arrangements
- Dx-11q signal relay
- Unknown database连接数据库错误
- 未来的 Web3会带来什么?
- dc_ Study and summary of labs--lab1
- JS to convert numbers into Chinese characters for output
- Uniapp official component clicking item is invalid, solution
- Sécurité et santé microbiennes, qu'est - ce que le traitement biologique?
猜你喜欢

DC學習筆記正式篇之零——綜述與基本流程介紹

uniapp官方组件点击item无效,解决方案

数字IC设计流程总结

qt5-MVC:数据可视化的层次揭秘

未来的 Web3会带来什么?

Basic knowledge II - Basic definitions related to sta

Microbiological health, why is food microbiological testing important

dc_ Study and summary of labs--lab1

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

Q play soft large toast to bring more comfortable sleep
随机推荐
Basic knowledge 3 - standard unit library
【动态规划】路径dp:931. Minimum Falling Path Sum
Basic knowledge II - Basic definitions related to sta
mysql插入\更新前+判断条件
Installing mongodb database in Windows Environment
面对产业互联网的时候,甚至还用消费互联网的方式和方法去落地和实践产业互联网
一些本质的区别
K210 site helmet
Split the linked list [take next first and then cut the linked list to prevent chain breakage]
未来的 Web3会带来什么?
The liquor and tourism sector recovers, and Yaduo continues to dream of listing. How far is it from "the first share of the new accommodation economy"?
Why build a personal blog
[leetcode] climb stairs [70]
Thinking brought by strictmode -strictmode principle (5)
Double position relay dls-5/2 dc220v
Complete software development process
Visual studio 2019 Download
ASCII、Unicode、GBK、UTF-8之间的关系
Dls-20 double position relay 220VDC
【模拟】922. Sort Array By Parity II