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 realize the method of character Imaging in Python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to achieve text imaging methods in Python, I hope you have something to gain after reading this article, let's discuss it together!

1. Preview of special effects

Before treatment

After treatment

After the details are enlarged

II. Program principle

1. Enter the text you want to hide

two。 Then write it on another blank piece of paper the same size as the photo.

3. Just fill the color of the text in the same position with the color in the same position on the photo.

4. And generate a new picture. Do you understand?

3. Program source code #! / usr/bin/env python# encoding: utf-8 from PIL import Image, ImageDraw, ImageFont class wordPicture:''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.font_size = 7 self.picture =' assets/picture.jpeg' def hello (self):''This is a welcome speech: return: self'' Print ('* * 50) print ('* 20 + 'text Imaging') print ('* 5 + 'Author: autofelix Date: 2022-01-06 13 print 14') print ('* * 50) return self def run (self):''The program entry' word = input ('enter what you want Said:') or'I like you 'resource = Image.open (self.picture) img_array = resource.load () image_new = Image.new (' RGB' Resource.size, (0,0,0) draw = ImageDraw.Draw (image_new) font = ImageFont.truetype ('/ System/Library/Fonts/PingFang.ttc', self.font_size) yield_word = self.character_generator (word) for y in range (0, resource.size [1], self.font_size): for x in range (0, resource.size [0]) Self.font_size): draw.text ((x, y), next (yield_word), font=font, fill=img_array [x, y], direction=None) image_new.convert ('RGB'). Save (' result.jpeg') def character_generator (self Text): while True: for i in range (len (text)): yield text [I] if _ _ name__ = ='_ _ main__': wordPicture (). Hello (). Run () finished reading this article I believe you have a certain understanding of "how to achieve text imaging in Python". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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

Development

Wechat

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

12
Report