5.20,福利一波,用文字拼一个自己最爱的人
程序员文章站
2022-06-01 15:53:12
...
功能:如题
原图:(略)
效果图:
代码:
import cv2
from PIL import Image, ImageDraw, ImageFont
def draw_text_on_pic(img_file, text_to_draw, font_path, font_size=10, char_space=2):
"""
:param img_file:目标图片
:param text_to_draw: 要绘制的文字
:param font_path: 字体目录
:param font_size: 字体大小
:param char_space: 字符间距,正数增大间距,负数减小间距
:return:无
"""
img_src = cv2.imread(img_file)
img_dst = Image.new("RGB", [img_src.shape[1], img_src.shape[0]], "white")
draw = ImageDraw.Draw(img_dst)
font = ImageFont.truetype(font_path, size=font_size)
for x in range(0, img_src.shape[0], font_size + char_space):
for y in range(0, img_src.shape[1], font_size + char_space):
draw.text(
[y, x],
text_to_draw[int(y / font_size + char_space) % len(text_to_draw)],
fill=(img_src[x][y][2], img_src[x][y][1], img_src[x][y][0]),
font=font
)
img_dst.save('保存结果-' + img_file)
draw_text_on_pic('老婆.jpg', "我爱你", "楷体.ttf", 10, 1)
上一篇: 实时通信的「聊天室」源码,Ctrl+c/v就可以用!!!
下一篇: Mysql PDO基础操作