将字符串写入位图图像

2024-05-15 03:12:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几个字符串要保存为分辨率为264*176的位图图像,以便在E-ink屏幕上显示(因为显然eInk显示器不能水平显示文本)。你知道吗

enter image description here

请注意,蓝色背景仅用于显示图像的尺寸。实际背景为白色。你知道吗

我试过PIL但没有成功。有人能推荐Python3的方法吗?你知道吗


Tags: 字符串图像文本pil屏幕尺寸水平分辨率
1条回答
网友
1楼 · 发布于 2024-05-15 03:12:22

下面是一个PIL示例

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

img = Image.new('RGB', (264, 176), color = (255, 255, 255))

font_path = '/usr/share/fonts/truetype/ubuntu/UbuntuMono-R.ttf'
font = ImageFont.truetype(font_path, 20)


draw = ImageDraw.Draw(img)
draw.text((15, 15), 'Is it your text?', font=font, fill=(0, 0, 0))

img.save('img_with_text.bmp', 'bmp')

Result

相关问题 更多 >

    热门问题