在Python中,可以使用Pillow库来生成占位图像。以下是一个示例代码:
from PIL import Image, ImageDraw, ImageFont
# 创建一个空白图像
width, height = 800, 600
image = Image.new('RGB', (width, height), 'gray')
# 创建绘图对象
draw = ImageDraw.Draw(image)
# 设置字体和文字
font_size = 48
font = ImageFont.truetype('Arial.ttf', font_size)
text = 'Placeholder'
# 计算文字的尺寸
text_width, text_height = draw.textsize(text, font=font)
# 计算文字的位置
x = (width - text_width) // 2
y = (height - text_height) // 2
# 绘制文字
draw.text((x, y), text, fill='white', font=font)
# 保存图像
image.save('placeholder.png')
这段代码使用Pillow库创建了一个800x600像素的灰色图像,然后在中心位置添加了文字“Placeholder”。最后将图像保存为placeholder.png。你可以根据需要调整图像大小、字体、文字内容等。