Valentine's Day

今天看猪哥的python公众号发现了一段好玩的代码,可以用来情人节给小姐姐~

先上效果图

1qKxyD.jpg

1qKvQO.jpg

由于图片不是高清,所以会有点模糊,只需要高清图即可

上代码~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from PIL import Image, ImageDraw, ImageFont
font_size = 7
text = "我喜欢你!"#可任意修改文字
img_path = "C://Users//Desktop//pics//fbb.jpg"#原图地址

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

img_new = Image.new("RGB", img_raw.size, (100, 100, 100))#像素设置(0,0,0)至(255,255,255)根据图片具体颜色设置吧
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("C://Users//Desktop//pics//fbb1.jpg")##输出图片
坚持原创技术分享,您的支持将鼓励我继续创作!