欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python特效之文字成像方法详解

程序员文章站 2022-03-02 23:12:20
目录一、特效预览二、程序原理三、程序源码一、特效预览处理前处理后细节放大后二、程序原理1.输入你想隐藏的文字2.然后写到另一张跟照片同等大小的空白纸张上3.将相同位置的文字的颜色用照片上相同位置的颜色...

一、特效预览

Python特效之文字成像方法详解

处理前

Python特效之文字成像方法详解

处理后

Python特效之文字成像方法详解

细节放大后

二、程序原理

1.输入你想隐藏的文字

2.然后写到另一张跟照片同等大小的空白纸张上

3.将相同位置的文字的颜色用照片上相同位置的颜色填充即可

4.然后生成新的图片你听懂了吗 

三、程序源码

#!/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 + '文字成像')
        print(' ' * 5 + 'author: autofelix  date: 2022-01-06 13:14')
        print('*' * 50)
        return self
 
    def run(self):
        '''
        the program entry
        '''
        word = input('请输入你想说的:') or '我钟意你'
 
        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()

到此这篇关于python特效之文字成像方法详解的文章就介绍到这了,更多相关python文字成像内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!