Python 使用 PIL 在其中绘制一个矩形和一个文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41405632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:53:47  来源:igfitidea点击:

Draw a rectangle and a text in it using PIL

pythonpython-3.ximage-processingpython-imaging-library

提问by ako25

I want to draw a rectangle and a text in it, here's a part of my code and it's a bit obfuscated:

我想在其中绘制一个矩形和一个文本,这是我的代码的一部分,它有点模糊:

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

  source_img = Image.open(file_name).convert("RGB")

  img1 = Image.new("RGBA", img.size, (0,0,0,0))
  draw1 = ImageDraw.Draw(watermark, "RGBA")
  draw1.rectangle(((0, 00), (100, 100)), fill="black")
  img_rectangle = Image.composite(img1, source_img, img1)

  draw2 = ImageDraw.Draw(img1, "RGBA")
  draw2.text((20, 70), "something123", font=ImageFont.truetype("font_path123"))

  Image.composite(img1, source_img, img1).save(out_file, "JPEG")

This draws them both, but they're separate: the text is under the rectangle. Whereas I want a text to be drawn inside the rectangle. How can I do that? Should I necessarilycompose them or what?

这会同时绘制它们,但它们是分开的:文本位于矩形下方。而我想在矩形内绘制一个文本。我怎样才能做到这一点?我一定要创作它们还是什么?

回答by furas

You can do it without composite()

你可以不用 composite()

from PIL import Image, ImageFont, ImageDraw, ImageEnhance

source_img = Image.open(file_name).convert("RGBA")

draw = ImageDraw.Draw(source_img)
draw.rectangle(((0, 00), (100, 100)), fill="black")
draw.text((20, 70), "something123", font=ImageFont.truetype("font_path123"))

source_img.save(out_file, "JPEG")


You can create empty image with size of button and put text on it and later put this image on source_img. This way long text will be cut to size of button.

您可以创建具有按钮大小的空图像并在其上放置文本,然后将此图像放在source_img. 这样长文本将被剪切到按钮的大小。

from PIL import Image, ImageFont, ImageDraw, ImageEnhance

source_img = Image.open("source.jpg").convert("RGBA")

# create image with size (100,100) and black background
button_img = Image.new('RGBA', (100,100), "black")

# put text on image
button_draw = ImageDraw.Draw(button_img)
button_draw.text((20, 70), "very loooooooooooooooooong text", font=ImageFont.truetype("arial"))

# put button on source image in position (0, 0)
source_img.paste(button_img, (0, 0))

# save in new file
source_img.save("output.jpg", "JPEG")


EDIT:I use ImageFont.getsize(text)to get text size and create button with correct size.

编辑:ImageFont.getsize(text)用来获取文本大小并创建具有正确大小的按钮。

from PIL import Image, ImageFont, ImageDraw, ImageEnhance

source_img = Image.open("input.jpg").convert("RGBA")


font = ImageFont.truetype("arial")

text = "very loooooooooooooooooong text"

# get text size
text_size = font.getsize(text)

# set button size + 10px margins
button_size = (text_size[0]+20, text_size[1]+20)

# create image with correct size and black background
button_img = Image.new('RGBA', button_size, "black")

# put text on button with 10px margins
button_draw = ImageDraw.Draw(button_img)
button_draw.text((10, 10), text, font=font)

# put button on source image in position (0, 0)
source_img.paste(button_img, (0, 0))

# save in new file
source_img.save("output.jpg", "JPEG")