枕头,添加和旋转图像中的文本

问题描述 投票:1回答:1

我想在图像中添加诸如“H”的文本。然后,旋转文本。我尝试在python中使用Pillow 6.0模块来执行此操作。这是我的代码:

import os
from PIL import Image
from PIL import ImageFont, ImageDraw, ImageOps

img_1 = Image.new("RGB", (100, 100), (255, 255, 255))
img_2 = Image.new("L", (100, 100), 255)

font = ImageFont.load_default()
font_size = 20
font = ImageFont.truetype("arial.ttf", font_size)
draw = ImageDraw.Draw(img_2)
draw.text((50, 50), "H", fill=0, font=font)
rot_im = img_2.rotate(45, expand=False)
img_1.paste(rot_im)
img_1.save('./generated_img/im_1.png')

I expected this:

enter image description here

But I got this:

enter image description here

Question:

  1. 如何将黑色部分制作为白色背景?
python python-3.x python-imaging-library
1个回答
3
投票

rotate有一个fillcolor参数*。您可以将其设置为“白色”。

rot_im = img_2.rotate(45, expand=False, fillcolor="white")

(* Qazxswpoi)

© www.soinside.com 2019 - 2024. All rights reserved.