Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to make the legendary digital screen

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Python to make the legendary digital screen". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Python to make the legendary digital screen.

Pillow library

This time we still need to use the pillow library we talked about last time.

Pillow libraries are installed using pip

Pip install pillow

Or

Pip install pillow-I "https://pypi.doubanio.com/simple/"

Code introduction

To introduce to you, what we mean by "digital screen" is to convert a picture into a character painting.

Character painting is a combination of a series of characters, we can think of characters as relatively large blocks of pixels, a character can represent a color, the more kinds of characters, the more colors can be displayed, the picture will have a more hierarchical sense.

The final display is the black and white character painting, so how to convert the color picture into black and white? Here you need to understand the concept of grayscale values.

Grayscale value: refers to the color depth of the point in the black-and-white image, generally ranging from 0 to 255, white is 255, black is 0, so the black-and-white picture is also called grayscale image.

We use the following formula to convert the RGB value of a pixel to a grayscale value:

Gray = 0.2126 * r + 0.7152 * g + 0.0722 * b

Different characters represent different grayscale values. The type and number of characters can be adjusted according to the desired effect. It is best to see obvious changes after going there. The last character is the best space character:

Ascii_char = list ("$@ B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\ | () 1 {} []?-_ + ~ iposit,\" ^ `'. ")

| | Code implementation |

From PIL import Image IMG='D:\ Code\ python\\ test.png' WIDTH=60 HEIGHT=45 ascii_char = list ("$@ B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\ | () 1 {} []?-_ + ~ isimlis,\" ^ `'. ") # maps 256grayscale to 70 characters def get_char Alpha=256): # alpha transparency if alpha==0: return''length=len (ascii_char) gray=int (0.2126*r+0.7152*g+0.0722*b) # calculate grayscale unit= (256.0mm1) / length return ascii_ charint [int (gray/unit)] # different grayscale corresponds to different characters # distinguish color blocks if _ _ name__=='__ by grayscale Main__': im=Image.open (IMG) im=im.resize ((WIDTH HEIGHT), Image.NEAREST) txt= "" for i in range (HEIGHT): for j in range (WIDTH): txt+=get_char (* im.getpixel ((jjwoni) txt+='\ n' print (txt) # write to the file with open ("output.txt",'w') as f: f.write (txt)

Let's generate a digital screen:

Thank you for your reading, the above is the content of "how to use Python to make the legendary digital screen". After the study of this article, I believe you have a deeper understanding of how to use Python to make the legendary digital screen. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report