from PIL import Image, ImageDraw, ImageFont, ImageEnhance
import numpy as np
# 定义图像的尺寸和背景颜色
width, height = 700, 400
white = (255, 255, 255) # 白色
green = (0, 139, 69) # 绿色
pink = (255, 99, 71) # 番茄红
background_color = green
# 创建一个新图像
image = Image.new("RGB", (width, height), background_color)
draw = ImageDraw.Draw(image)
# 定义要显示的文本和文字颜色
text = "我是大标题"
text_color = (0, 0, 0) # 黑色
# 加载字体文件并设置字体大小
font_size = 36
font = ImageFont.truetype("simsun.ttc", font_size) # simsun.ttc 包含汉字 arial.ttf
# 计算文本的坐标使其居中显示
text_width, text_height = draw.textsize(text, font=font)
x = (width - text_width) // 2
y = text_height - 10
current_height = y
# 在图像上绘制文本
draw.text((x, y), text, fill=text_color, font=font)
# 第二行
font_size = 24
font = ImageFont.truetype("simsun.ttc", font_size) # simsun.ttc 宋体,包含汉字 arial.ttf
# 计算文本的坐标使其居中显示
text_width, text_height = draw.textsize(text, font=font)
x = font_size * 2
y = text_height + current_height + 12
current_height = y
text = "小标题1:"
# 在图像上绘制文本
draw.text((x, y), text, fill=text_color, font=font)
# 第三行
font_size = 20
font = ImageFont.truetype("simsun.ttc", font_size) # simsun.ttc 宋体,包含汉字 arial.ttf
# 计算文本的坐标使其居中显示
text_width, text_height = draw.textsize(text, font=font)
x = font_size * 2 + 20
y = text_height + current_height + 6
current_height = y
text = "1、可以填写更多文字,但不会自动换行 \n2、可以使用/\n 换行符等。\n3、更多内容"
# 在图像上绘制文本
draw.text((x, y), text, fill=text_color, font=font)
# 第四行
font_size = 24
font = ImageFont.truetype("simsun.ttc", font_size) # simsun.ttc 宋体,包含汉字 arial.ttf
# 计算文本的坐标使其居中显示
text_width, text_height = draw.textsize(text, font=font)
x = font_size * 2
y = text_height + current_height
current_height = y
text = "小标题2:"
# 在图像上绘制文本
draw.text((x, y), text, fill=text_color, font=font)
# 第五行
font_size = 20
font = ImageFont.truetype("simsun.ttc", font_size) # simsun.ttc 宋体,包含汉字 arial.ttf
# 计算文本的坐标使其居中显示
text_width, text_height = draw.textsize(text, font=font)
x = font_size * 2 + 20
y = text_height + current_height + 6
current_height = y
text = "我是京南网服小哥\n学习更多知识有利于更好的服务客户"
# 在图像上绘制文本
draw.text((x, y), text, fill=text_color, font=font)
# 第六行
font_size = 20
text_color = white # 黑色
font = ImageFont.truetype("simsun.ttc", font_size) # simsun.ttc 宋体,包含汉字 arial.ttf
# 计算文本的坐标使其居中显示
text_width, text_height = draw.textsize(text, font=font)
x = font_size * 2
y = text_height + current_height + 12
current_height = y
text = "其它文字信息"
# 在图像上绘制文本
draw.text((x, y), text, fill=text_color, font=font)
# 保存图像
# 增强图片对比度
enhance = ImageEnhance.Contrast(image)
enhance = ImageEnhance.Brightness(image)
image = enhance.enhance(1.0 / (1 - 0.2))
# image.save("card.png")
# ----------------------------------------
# 合成 其它图片 到 图片中
er_image = Image.open("er_zzz666.png")
# 创建绘图对象
er_draw = ImageDraw.Draw(image)
def combine(origin_photo, text_pic, alpha=0, out_name='card_out.jpg'):
"""
为图片添加水印并保存
origin_photo: 原图内容
text_pic: 要添加的水印图片
alpha:水印的不透明度
out_name: 输出图片的文件名
"""
# 合并水印图片和原图
print(origin_photo.size)
text_pic = text_pic.resize(origin_photo.size)
# text_pic = text_pic.convert('RGBA')
print(text_pic, origin_photo)
# out = Image.blend(origin_photo, text_pic, alpha) # 混合 适合水印
out = Image.alpha_composite(origin_photo.convert('RGBA'), text_pic.convert('RGBA')) # 叠加 mode必须是RGBA
out = out.convert('RGB')
# # 增强图片对比度
# enhance = ImageEnhance.Contrast(out)
# out = enhance.enhance(1.0 / (1 - alpha))
# out.save(out_name)
out.show()
def tiepian(back_img, front_img):
# 将图片转换为numpy数组
back_img_array = np.asarray(back_img)
front_img_array = np.asarray(front_img)
print(back_img_array, front_img_array)
# 将待贴图片贴到原始图片上
back_img_array[10:210, 400:600] = front_img_array[:200, :200, :3] * (
front_img_array[:200, :200, 3:] / 255.0) + back_img_array[10:210, 400:600] * (
1.0 - front_img_array[:200, :200, 3:].astype(float) / 255.0)
# ValueError: operands
# could
# not be
# broadcast
# together
# with shapes(200, 200, 3)(200, 200, 0)
# 这种方式待进一步深入学习,比如:照片加章、人像换脸等更多应用场景
# 显示并保存结果图片
result_img = Image.fromarray(back_img_array)
result_img.show()
# result_img.save('result.jpg')
combine(image, er_image)
# tiepian(image, er_image)