下面是一个示例代码,它假设你已经安装了Pillow库来处理图像。在运行代码之前,确保将模板图片和目标图片保存在同一个文件夹中,并替换文件名和目标文本,以在图像上居中显示:



```python

from PIL import Image, ImageDraw, ImageFont



# 打开模板图片和目标图片

template = Image.open("template.png")

target = Image.open("target.png")



# 获取目标图片和模板图片的宽度和高度

target_width, target_height = target.size

template_width, template_height = template.size



# 在模板图片上居中添加文本

text = "Your text here"

font_size = 50

font = ImageFont.truetype("arial.ttf", font_size)

draw = ImageDraw.Draw(template)

text_width, text_height = draw.textsize(text, font=font)

text_x = (template_width - text_width) / 2

text_y = (template_height - text_height) / 2

draw.text((text_x, text_y), text, font=font, fill=(255, 255, 255))



# 将模板图片转换为半透明

alpha_level = 128

alpha = Image.new("L", template.size, alpha_level)

template.putalpha(alpha)



# 将模板图片居中合并到目标图片

target.paste(template, ((target_width - template_width) // 2, (target_height - template_height) // 2), template)



# 保存合并后的图片

target.save("merged.png")

```



请注意,此代码仅在使用指定的Arial字体和默认字体大小和颜色的情况下测试过,因此可能需要根据自己的情况进行调整。

(本文内容根据网络资料整理和来自用户投稿,出于传递更多信息之目的,不代表本站其观点和立场。也不对其真实性、可靠性承担任何法律责任,特此声明!)

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部