Python中如何实现文字成像方法

发布时间:2022-01-14 14:12:57 作者:小新
来源:亿速云 阅读:136

小编给大家分享一下Python中如何实现文字成像方法,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

一、特效预览

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中如何实现文字成像方法”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Python基于Tesseract实现识别文字功能的方法
  2. python中怎么实现文字识别功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:C#怎么使用Twain协议实现扫描仪连续扫描功能

下一篇:springboot整合quartz定时任务框架的方法是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》