Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the method of character Imaging in Python

Shulou Source: shulou.com Published: 2022-06-02 02:31:36 10月22日 Update

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!

Tags: Text imaging method same position photo program article color input pre-processing principle picture size finished more source code special effects knowledge program source code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Linux Redmi OPPO Reno Apple