添加文本并将其旋转 45 度 PIL/Pillow

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

我尝试添加文本并旋转它 我需要将文本旋转45度,但是Image.ROTATE_90不存在

from PIL import Image, ImageDraw, ImageFont

image = Image.open("good_watermark.jpg")

width, height = image.size
font = ImageFont.truetype("arial.ttf", 25)
font = ImageFont.TransposedFont(font, orientation=Image.ROTATE_90)

drawer = ImageDraw.Draw(image)
drawer.text((height/2, width/3), "@channel", font=font, fill='black')

image.save('new_img.jpg')
image.show()

我试图创建一个具有透明背景和文本的单独图像并将其添加到主图像中。但由于某种原因,文字没有出现

from PIL import Image, ImageDraw, ImageFont

background_image = Image.open("white_img.jpeg")
width, height = background_image.size

text_image = Image.new("RGBA", (width, height), (0, 0, 0, 0))

draw = ImageDraw.Draw(text_image)

font = ImageFont.truetype("arial.ttf", 40)
draw.text((height/2, width/3), "TEXT", font=font)

rotated_text_image = text_image.rotate(45)
background_image.paste(rotated_text_image, (0, 0), mask=rotated_text_image)

background_image.save("new_img.jpg")

我也尝试使用这里的解决方案 如何使用Python的PIL以一定角度绘制文本? 在图像中放置、添加和旋转文本

我尝试了很多事情,但我不明白我做错了什么。我很高兴得到任何答复

python image text rotation python-imaging-library
1个回答
0
投票
from PIL import Image, ImageFont, ImageDraw

text = '@estate_bot'
font = ImageFont.truetype('arial.ttf', 30)

image1 = Image.open('good_watermark.jpg')
draw1 = ImageDraw.Draw(image1)
width, height = image1.size

image2 = Image.new('RGBA', (width, height), (0, 0, 0, 0))
draw2 = ImageDraw.Draw(image2)
draw2.text((height/2, width/3), text=text, font=font, fill=(255, 255, 255, 150))  
# (255, 255, 255, 150) 4th parameter - transparency

image2 = image2.rotate(38)

px, py = 10, 10
sx, sy = image2.size
image1.paste(im=image2, box=(px, py, px + sx, py + sy), mask=image2)

image1.save('new_img.jpg', quality=100)
image1.show()
© www.soinside.com 2019 - 2024. All rights reserved.