-
Is there anyway to draw each character by directly rotating counterclockwise 90 degree? |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 6 replies
-
Pillow doesn't offer a single API to draw rotated text. For an example of drawing text onto a temporary image, rotating that image, and then pasting it onto another, see #5738 (comment) |
Beta Was this translation helpful? Give feedback.
-
Thank you.
|
Beta Was this translation helpful? Give feedback.
-
Thank you. But a whole is limited to whole line angle only. |
Beta Was this translation helpful? Give feedback.
-
How to update this line to avoid error? def draw_rotated_text(image, font, text, angle, x, y):
txt = Image.new(image.mode, font.getsize(text))
d = ImageDraw.Draw(txt)
d.text((0, 0), text, font=font, fill=(255, 0, 0))
txt = txt.rotate(angle, expand=1)
image.paste(txt, (int(x - txt.width/2), int(y - txt.height/2)), txt) |
Beta Was this translation helpful? Give feedback.
-
Thank you. Another error poped here. d.text((0, 0), text, font=font, fill=(255, 0, 0))
|
Beta Was this translation helpful? Give feedback.
-
Thank you. draw_rotated_text(image,font_family, '我', 0, 0+font_size/2, image.height/2)
draw_rotated_text(image, font_family, "们", 90, 0+font_size/2+font_size*1, image.height/2)
draw_rotated_text(image, font_family, "e", 120, 0+font_size/2+font_size*2, image.height/2) |
Beta Was this translation helpful? Give feedback.
-
I editted your original code, and now another error poped.
from PIL import Image, ImageDraw, ImageFont, ImageFilter
def draw_rotated_text(image, font, text, angle, x, y):
# txt = Image.new(image.mode, font.getsize(text))
left, top, right, bottom = font.getbbox(text)
width, height = right - left, bottom - top
txt = Image.new(image.mode, (width, height))
d = ImageDraw.Draw(txt)
d.text((0, 0), text, font=font, fill=(255, 0, 0))
# d.text((0, 0), text, font=font, fill=255)
txt = txt.rotate(angle, expand=1)
image.paste(txt, (int(x - txt.width/2), int(y - txt.height/2)), txt)
def image_text():
image = Image.open(r'F:\bg1.jpg')
# font = ImageFont.truetype('demo.ttf', 50)
font_size = 50
font0 = ImageFont.truetype(r'F:\fonts\Arial Unicode MS.ttf', size=font_size - 5)
fontpath1 = r"F:\fonts\NotoOldShapeNormal.TTF"
font1 = ImageFont.truetype(fontpath1, size=font_size - 5)
# font_family = ImageFont.FreeTypeFontFamily(font0, font1, font2)
font_family = ImageFont.FreeTypeFontFamily(font0, font1)
draw_rotated_text(image,font_family, '我', 30, 0+font_size/2, image.height/2)
draw_rotated_text(image, font_family, "们", 50, 0+font_size/2+font_size*1, image.height/2)
draw_rotated_text(image, font_family, "e", 80, 0+font_size/2+font_size*2, image.height/2)
image.show()
if __name__ == '__main__':
image_text() |
Beta Was this translation helpful? Give feedback.
-
fonts.zip |
Beta Was this translation helpful? Give feedback.
-
Ok. Result is below, seems half cut for Chinese characters, and missing English letter. |
Beta Was this translation helpful? Give feedback.
Pillow doesn't offer a single API to draw rotated text. For an example of drawing text onto a temporary image, rotating that image, and then pasting it onto another, see #5738 (comment)