Python生成内涵图片

先看案例

原图

 

生成后(放大看看,嘿嘿嘿)

 

 

上代码:

from PIL import Image, ImageDraw, ImageFont

font_size = 7
text = "我喜欢你!"
img_path = "yy.jpg"

img_raw = Image.open(img_path)
img_array = img_raw.load()

img_new = Image.new("RGB", img_raw.size, (0, 0, 0))
draw = ImageDraw.Draw(img_new)
font = ImageFont.truetype('C:/Windows/fonts/Dengl.ttf', font_size)

def character_generator(text):
    while True:
        for i in range(len(text)):
            yield text[i]

ch_gen = character_generator(text)

for y in range(0, img_raw.size[1], font_size):
    for x in range(0, img_raw.size[0], font_size):
        draw.text((x, y), next(ch_gen), font=font, fill=img_array[x, y], direction=None)

img_new.convert('RGB').save("save_yy.jpeg")

 

关注公众号:Python爬虫数据分析挖掘,学习更多python知识

 

 

 

posted @ 2020-07-06 17:52  Python研究者  阅读(258)  评论(0编辑  收藏  举报