程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Text imaging of Python special effects

編輯:Python

What is?   Character imaging

Pass the original picture through python After processing , Generated entirely by plaintext Composition of images .

One 、 Special effects preview

Before processing
After processing
After the detail is enlarged

Two 、 Principle of procedure

  • Enter the text you want to hide
  • Then write it on another blank piece of paper the same size as the photo
  • Fill the color of the text at the same position with the color at the same position on the photo
  • Then generate a new picture
Do you understand?

3、 ... and 、 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 + ' Character imaging ')
print(' ' * 5 + 'Author: autofelix Date: 2022-01-06 13:14')
print('*' * 50)
return self
def run(self):
'''
The program entry
'''
word = input(' Please enter what you want to say :') 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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved