代码说明:
新手可改:
font_size :可以调节文字的大小
text :显示在图片里面的文字
img_path :导入图片的位置
Image.new(mode,size,color):mode用于新图像的模式,size调节图片大小,color调节颜色
效果如下:
代码如下:
from PIL import Image, ImageDraw, ImageFont
font_size = 10
text = "靓仔"
img_path = "0.jpg"
img_raw = Image.open(img_path)
img_array = img_raw.load()
img_new = Image.new("RGB", img_raw.size, (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("123.jpg")