[Python] 纯文本查看 复制代码 from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype('c:/windows/Fonts/Arial.ttf', 24)
# image: 图片 text:要添加的文本 font:字体
def add_text_to_image(image, text, font=font):
rgba_image = image.convert('RGBA')
text_overlay = Image.new('RGBA', rgba_image.size, (255, 255, 255, 0))
image_draw = ImageDraw.Draw(text_overlay)
text_size_x, text_size_y = image_draw.textsize(text, font=font)
# 设置文本文字位置
print(rgba_image)
text_xy = (rgba_image.size[0] - text_size_x, rgba_image.size[1] - text_size_y)
# 设置文本颜色和透明度
image_draw.text(text_xy, text, font=font, fill=(76, 234, 124, 180))
image_with_text = Image.alpha_composite(rgba_image, text_overlay)
return image_with_text
if __name__ == '__main__':
im_before = Image.open("yy.jpg")
# im_before.show()
W_text = input('请输入水印文字:').strip()
im_after = add_text_to_image(im_before, W_text)
im_after.show()
# im.save('im_after.jpg') |