Created
April 20, 2017 04:55
-
-
Save wwwins/7d4eee5331d88549671e57720d362196 to your computer and use it in GitHub Desktop.
PIL image font
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# | |
# imagefont.py | |
# | |
from PIL import Image, ImageDraw, ImageFont | |
# image = Image.open('cats.png') | |
# width, height = image.size | |
image = Image.new("RGBA",(500,1000),(0,0,0,255)) | |
width, height = image.size | |
draw = ImageDraw.Draw(image) | |
text = "abcdefg" | |
multiline_text = "\nabcdefg\nABCDEFG\n123456789" | |
uni_text = "測試一" | |
multiline_uni_text = "\n測試二\n測試三\n測試四" | |
font = ImageFont.truetype("/Library/Fonts/PingFang.ttc", 36) | |
textwidth, textheight = draw.textsize(text, font) | |
print(textwidth,textheight) | |
textwidth, textheight = draw.multiline_textsize(multiline_text, font) | |
print(textwidth,textheight) | |
# draw watermark in the bottom right corner | |
draw.text((50,10),unicode(text, 'UTF-8'), font=font, fill=(255,155,155,128)) | |
draw.multiline_text((50,10),unicode(multiline_text, 'UTF-8'), font=font, fill=(255,155,155,128), align="left") | |
draw.text((50,200),unicode(uni_text, 'UTF-8'), font=font, fill=(255,155,155,128)) | |
draw.multiline_text((50,200),unicode(multiline_uni_text, 'UTF-8'), font=font, fill=(255,155,155,128), align="left") | |
image.save('text.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment