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

边栏推荐
- Exploration and practice of "flow batch integration" in JD
- 个人博客搭建与美化
- 【模拟】922. Sort Array By Parity II
- [问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号
- Mustache syntax
- Draw some interesting figures with flutter's canvas
- 孙宇晨接受瑞士媒体Bilan采访:熊市不会持续太久
- Dls-42/6-4 dc110v double position relay
- Kongyiji's first question: how much do you know about service communication?
- One of the basics - overview of sta Basics
猜你喜欢

Call the classic architecture and build the model based on the classic

Basic knowledge II - Basic definitions related to sta

Dls-42/6-4 dc110v double position relay

关于白盒测试,这些技巧你得游刃有余~

Xjy-220/43ac220v static signal relay

45岁程序员告诉你:程序员为什么要跳槽,太真实...
![[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!]

1500w播放下还藏着什么热点?B站2个未来趋势你不得错过

Q play soft large toast to bring more comfortable sleep

ESP8266 RC522
随机推荐
日志 logrus第三方库的使用
未来的 Web3会带来什么?
One of the basics - overview of sta Basics
Visual studio 2019 Download
What are the functions of soil microorganisms in microbial detection?
Pre training / transfer learning of models
使用 C# 创造 ASCII 艺术
ESP8266 RC522
Microbial safety and health, what is biological treatment?
zabbix如何配置告警短信?(预警短信通知设置流程)
flutter报错 -- The argument type ‘Function‘ can‘t be assigned to the parameter type ‘void Function()?‘
Kongyiji's first question: how much do you know about service communication?
[network packet loss and network delay? This artifact can help you deal with everything!]
Thinking brought by strictmode -strictmode principle (5)
neo4j安装、运行以及项目的构建和功能实现
ASCII、Unicode、GBK、UTF-8之间的关系
软件开发完整流程
MATLAB 最远点采样(FPS改进版)
使用StrictMode-StrictMode原理(1)
一站式洞察行业热点,飞瓜数据B站新功能「流量大盘」上线!