Pillow. Как вывести текст на картинке?

try:
		cid = m.chat.id
		bot.send_chat_action(cid, 'typing')
		mode = "RGB"
		size = 128,128
		base = Image.new(mode, size, color=0).convert('RGBA')
		# make a blank image for the text, initialized to transparent text color
		txt = Image.new('RGBA', base.size, (255,255,255,0))
		# get a font
		fnt = ImageFont.load_default()
		# get a drawing context
		d = ImageDraw.Draw(txt)
		# draw text, half opacity
		d.text((10,10), "Hello", font=fnt, fill=(255,255,255,128))
		# draw text, full opacity
		d.text((10,60), "World", font=fnt, fill=(255,255,255,255))
		out = Image.alpha_composite(base, txt)
		img = out.show()
		bot.send_photo(cid, img)
	except Exception as e:
		bot.send_message(adminid, '\xE2\x9A\xA0 Ошибка: ' +str(e))


Выдаёт ошибку 'nonetype' object has no attribute 'read'
  • Вопрос задан
  • 3914 просмотров
Решения вопроса 1
lavr_fedotov
@lavr_fedotov Автор вопроса
Решил проблему так:
txt = Image.open("im.png")
		fnt = ImageFont.truetype("font.ttf", 36)
		d = ImageDraw.Draw(txt)
		d.text((140,40), 'hello', font=fnt, fill=(0,0,0,255))
		del d
		txt.save("file.png", "PNG")
		img = open('file.png', 'rb')
		bot.send_photo(cid, img)
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы