Python 使用PIL绘制矩形和其中的文本

Python 使用PIL绘制矩形和其中的文本,python,python-3.x,image-processing,python-imaging-library,Python,Python 3.x,Image Processing,Python Imaging Library,我想在其中画一个矩形和一个文本,这是我代码的一部分,有点模糊: 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 = Ima

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

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")
这会同时绘制它们,但它们是分开的:文本位于矩形下方。而我希望在矩形内绘制一个文本。
我该怎么做?我应该必要地编写它们还是做什么?

你可以不用
composite()


您可以创建大小为按钮的空图像,并在其上放置文本,然后将此图像放置在
source\u 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")

编辑:我使用
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")

你不能用一个图像来做这个吗?draw1?@furas,我可以吗?它不会在矩形内绘制文本。它对我有用-我在矩形内获取文本。可能您检查了错误的图像-删除所有旧图像并再次运行。否,图像是正确的。它如何知道(20,70)是矩形内的坐标而不是整个图像内的坐标?(20,70)是图像内的坐标。但是(20, 70)是在矩形((0, 00),(100, 100))内,所以它将文本放在矩形中。我怎样才能得到绘图?文本()以考虑(20, 70)是相对于矩形的坐标?
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")