开发者

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

开发者 https://www.devze.com 2022-12-08 12:39 出处:网络 作者: autofelix
目录一、特效预览二、程序原理三、程序源码一、特效预览 处理前 处理后 细节放大后
目录
  • 一、特效预览
  • 二、程序原理
  • 三、程序源码

一、特效预览

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

处理前

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

处理后

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

细节放大后

二、程序原理

1.输入你想隐藏的文字

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

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

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

三、程序源码

#!/usr/bin/env python
# encoding: utf-8
 
from PIL import Image, ImageDraw, ImageFont
 
class wordPicture:
    www.cppcns.com'''
     This is a main Class, the filwww.cppcns.come 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):
 http://www.cppcns.com       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(y编程客栈ield_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文字成像内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号