怎样用python在图片上写出半透明字体
时间: 2023-06-25 16:03:22 浏览: 91
你可以使用Python的PIL库来实现在图片上写出半透明字体。下面是一个简单的示例代码:
```python
from PIL import Image, ImageDraw, ImageFont
# 打开图片并创建绘图对象
image = Image.open('image.jpg')
draw = ImageDraw.Draw(image)
# 设置字体和文字内容
font = ImageFont.truetype('arial.ttf', 36)
text = 'Hello, World!'
# 设置文字位置和颜色,并绘制半透明文字
x, y = 50, 50
text_color = (255, 255, 255, 128) # RGBA格式,最后一个数值为透明度
draw.text((x, y), text, font=font, fill=text_color)
# 保存图片
image.save('result.jpg')
```
运行代码后,你会得到一张名为`result.jpg`的图片,上面写有半透明的`Hello, World!`文字。你可以根据需要修改字体、文字内容、位置、颜色等参数。