In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to write image code composed of characters in python". In daily operation, I believe that many people have doubts about how to write image code composed of characters in python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to write image codes composed of characters in python". Next, please follow the editor to study!
1. Preview of special effects
Before treatment
After treatment
After the details are enlarged
II. Program principle
Map 256 grayscale of the picture to the corresponding characters
That is, the RGB value is converted to the corresponding character.
Then write the characters to the file.
Do you understand me?
Third, the program source code #! / usr/bin/env python# encoding: utf-8from PIL import Image class charsetPicture:''This is a main Class, the file contains all documents. One document contains paragraphs that have several sentences It loads the original file and converts the original file to new content Then the new content will be saved by this class''def _ _ init__ (self): self.char = list ("$@ B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\ | () 1 {} []?-_ + ~ itransitive init__,\" ^ `'. ") self.width = 160self.height = 60 def hello (self):''This is a welcome speech: return: self' 'print (' *'* 50) print ('* 18 + 'convert picture to character picture') print ('* 5 + 'Author: autofelix Date: 2022-01-07 1314' ) print ('* * 50) return self def get_char (self R, g, b, alpha=256):''map 256 grayscale to 70 characters That is, the character-to-character function of RGB: alpha: transparency: return: self'if alpha = = 0: return''length = len (self.char) gray = int (0.2126 * r + 0.7152 * g + 0.0722 * b) unit = (256.0 + 1) / length return self.char [int ( Gray / unit)] def run (self):''The program entry''im = Image.open ('assets/aaa.jpeg') im = im.resize ((self.width) Self.height), Image.NEAREST) txt =''for i in range (self.height): for j in range (self.width): txt + = self.get_char (* im.getpixel ((j, I)) txt + ='\ n' print (txt) with open ('handler.txt') 'w') as f: f.write (txt) if _ _ name__ = ='_ main__': charsetPicture (). Hello (). Run () so far On the "python character composition of image code how to write" study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.