本文整理汇总了Python中PIL.ImageFont.truetype方法的典型用法代码示例。如果您正苦于以下问题:Python ImageFont.truetype方法的具体用法?Python ImageFont.truetype怎么用?Python ImageFont.truetype使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PIL.ImageFont
的用法示例。
在下文中一共展示了ImageFont.truetype方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: displayImageFileOnLCD
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def displayImageFileOnLCD(filename):
print 'displays ', filename
title = 'Review Mode'
# resize/dither to screen resolution and send to LCD
image = Image.open(filename)
im_width, im_height = image.size
if im_width < im_height:
image = image.rotate(90)
image.thumbnail(S_SIZE, Image.ANTIALIAS)
image_sized = Image.new('RGB', S_SIZE, (0, 0, 0))
image_sized.paste(image,((S_SIZE[0] - image.size[0]) / 2, (S_SIZE[1] - image.size[1]) / 2))
# draw filename
draw = ImageDraw.Draw(image_sized)
font = ImageFont.truetype('arial.ttf', 18)
draw.rectangle([(0, 0), (115, 22)], fill=(255,255,255), outline=(0,0,0))
draw.text((2, 2), title, fill='black', font=font)
draw.rectangle([(279, 217), (399, 239)], fill=(255,255,255), outline=(0,0,0))
draw.text((290, 218), filename, fill='black', font=font)
# display on LCD
image_sized = ImageOps.invert(image_sized)
image_sized = image_sized.convert('1') # convert image to black and white
lcd.write(image_sized.tobytes())
示例2: create_image_from_text
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def create_image_from_text(in_text):
colours = (255, 255, 250)
font_file = '/usr/share/fonts/truetype/freefont/FreeSansBold.ttf'
font_size = 12
font = ImageFont.truetype(font_file, font_size)
w, h = font.getsize(in_text)
text_x, text_y = width, 0
text_width, text_height = width, 0
text_width += w + width # add some padding so the ip scrolls off the unicorn hat
text_height = max(text_height, h, 16) # no more than the size of the unicorn hat
image = Image.new('RGB', (text_width, text_height), (0, 0, 0))
draw = ImageDraw.Draw(image)
draw.text((text_x, text_y), in_text, colours, font=font)
return (image, text_width)
# DISPLAY
示例3: draw_text
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def draw_text(
img: Image,
text: str,
location: tuple = (0, 0),
text_color=(0, 0, 0)
) -> Image:
draw = ImageDraw.Draw(img)
try:
# For Linux
font = ImageFont.truetype("DejaVuSans.ttf", 20)
except Exception:
logger.warning("No font DejaVuSans; use default instead")
# For others
font = ImageFont.load_default()
draw.text(location, text, font=font, fill=text_color)
return img
示例4: draw
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def draw(self, image):
fontpath = FONTPATH[ random.sample(range(2),1)[0] ]
color = (self.color[0], self.color[1], self.color[2], 255)
font = ImageFont.truetype( fontpath , randint(25, 27) * 10)
text = Image.new("RGBA", (250, 300), (0, 0, 0, 0))
textdraw = ImageDraw.Draw(text)
textdraw.text((0, 0), str(self.number), font=font, fill=color)
#textdraw.text((0, 0), 'j', font=font, fill=color)
text = text.rotate(self.angle, expand=True)
text = text.resize((int(text.size[0] / 10), int(text.size[1] / 10)))
base = int(self.priority * (200 / 6))
rand_min = (self.offset - base - 2) if (self.offset - base - 2) >= -15 else -15
rand_min = 0 if self.priority == 0 else rand_min
rand_max = (33 - text.size[0]) if self.priority == 5 else (33 - text.size[0] + 10)
try:
displace = randint(rand_min, rand_max)
except:
displace = rand_max
location = (base + displace, randint(3, 23))
self.next_offset = location[0] + text.size[0]
image.paste(text, location, text)
# plt.imshow(image)
示例5: make_photo
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def make_photo(self, dir_):
"""生成验证码
:param dir_: 存放验证码照片的文件夹
"""
from PIL import Image # 安装pillow: pip install pillow
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageFilter
image = Image.new('RGB', (self.width, self.height), (255, 255, 255))
font = ImageFont.truetype('arial.ttf', 36)
draw = ImageDraw.Draw(image)
for w in range(self.width):
for h in range(self.height):
draw.point((w, h), fill=self.color1())
for index, t in enumerate(self.flag):
draw.text(((self.width - 10) // self.number * index + 10, 10), t, font=font, fill=self.color2())
image = image.filter(ImageFilter.BLUR)
image.save(dir_ + sep + ''.join(self.flag) + '.jpg', 'jpeg')
return image
示例6: drawCaption
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def drawCaption(img, caption):
img_txt = Image.fromarray(img)
# get a font
fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 50)
# get a drawing context
d = ImageDraw.Draw(img_txt)
# draw text, half opacity
d.text((10, 256), 'Stage-I', font=fnt, fill=(255, 255, 255, 255))
d.text((10, 512), 'Stage-II', font=fnt, fill=(255, 255, 255, 255))
if img.shape[0] > 832:
d.text((10, 832), 'Stage-I', font=fnt, fill=(255, 255, 255, 255))
d.text((10, 1088), 'Stage-II', font=fnt, fill=(255, 255, 255, 255))
idx = caption.find(' ', 60)
if idx == -1:
d.text((256, 10), caption, font=fnt, fill=(255, 255, 255, 255))
else:
cap1 = caption[:idx]
cap2 = caption[idx+1:]
d.text((256, 10), cap1, font=fnt, fill=(255, 255, 255, 255))
d.text((256, 60), cap2, font=fnt, fill=(255, 255, 255, 255))
return img_txt
示例7: drawCaption
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def drawCaption(self, img, caption):
img_txt = Image.fromarray(img)
# get a font
fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 50)
# get a drawing context
d = ImageDraw.Draw(img_txt)
# draw text, half opacity
d.text((10, 256), 'Stage-I', font=fnt, fill=(255, 255, 255, 255))
d.text((10, 512), 'Stage-II', font=fnt, fill=(255, 255, 255, 255))
if img.shape[0] > 832:
d.text((10, 832), 'Stage-I', font=fnt, fill=(255, 255, 255, 255))
d.text((10, 1088), 'Stage-II', font=fnt, fill=(255, 255, 255, 255))
idx = caption.find(' ', 60)
if idx == -1:
d.text((256, 10), caption, font=fnt, fill=(255, 255, 255, 255))
else:
cap1 = caption[:idx]
cap2 = caption[idx+1:]
d.text((256, 10), cap1, font=fnt, fill=(255, 255, 255, 255))
d.text((256, 60), cap2, font=fnt, fill=(255, 255, 255, 255))
return img_txt
示例8: render_idn
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def render_idn(text, font_name, font_size):
width = 500
height = 100
bgcolor = (255, 255, 255, 255)
color = (0,0,0, 255)
image = Image.new("RGBA", (width, height), bgcolor)
draw = ImageDraw.Draw(image)
font = ImageFont.truetype(font_name, font_size, encoding="unic")
draw.text((0, 0), text, font=font, fill=color)
out_file = tempfile.NamedTemporaryFile(suffix=".png")
image.save(out_file.name)
return out_file
示例9: get_font_size
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def get_font_size(size, font_file, text):
"""
指定したテキストを画像に配置する時にいい感じのフォントサイズを返す
:params size: 画像の幅と高さの短い方
:params font_file: フォントファイルのフルパス
:params text: 描画する文字列(LGTM等)
:return: フォントサイズ
"""
# フォントのサイズを5ポイント刻みで大きくする
for font_size in range(10, 200, 5):
font = ImageFont.truetype(font_file, font_size, encoding="utf-8")
# テキストの描画サイズを取得
width, height = font.getsize(text)
# テキストの幅が、画像の短い方の半分のサイズを越えたら終了
if width > size / 2:
break
return font_size
示例10: find_font_size
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def find_font_size(draw, text_lines, max_width, max_height, spacing):
# start with a default font size that should be large enough to be too large
font_size = 35
# reload the font until the word fits or the font size would be too small
while True:
font = ImageFont.truetype(random.choice(FONTS), size=font_size, encoding='unic')
text_width, text_height = draw.multiline_textsize(text_lines, font, spacing=spacing)
if text_width <= max_width and text_height <= max_height:
return font, (text_width, text_height)
font_size -= 1
if font_size <= 1:
raise ValueError('Can not draw Text on given image')
示例11: __init__
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def __init__(self, base_font_ttf: str, font_size: int):
self.font_name = base_font_ttf if not base_font_ttf.endswith('.ttf') else base_font_ttf[:-4]
self.default_font = Font(ImageFont.truetype(self.font_name + '.ttf', size=font_size))
def font_or_default(ttf):
try:
return Font(ImageFont.truetype(ttf, size=font_size))
except OSError:
print("Error loading font {}. Using default font.".format(ttf))
return self.default_font
self.variants = [
self.default_font,
font_or_default(self.font_name + '-Bold.ttf'),
font_or_default(self.font_name + '-Italic.ttf'),
font_or_default(self.font_name + '-BoldItalic.ttf'),
]
示例12: main
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def main():
epd = epd7in5.EPD()
epd.init()
# For simplicity, the arguments are explicit numerical coordinates
image = Image.new('1', (EPD_WIDTH, EPD_HEIGHT), 1) # 1: clear the frame
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 24)
draw.rectangle((0, 6, 640, 30), fill = 0)
draw.text((200, 10), 'e-Paper demo', font = font, fill = 255)
draw.rectangle((200, 80, 600, 280), fill = 0)
draw.arc((240, 120, 580, 220), 0, 360, fill = 255)
draw.rectangle((0, 80, 160, 280), fill = 255)
draw.arc((40, 80, 180, 220), 0, 360, fill = 0)
epd.display_frame(epd.get_frame_buffer(image))
image = Image.open('monocolor.bmp')
epd.display_frame(epd.get_frame_buffer(image))
# You can get frame buffer from an image or import the buffer directly:
#epd.display_frame(imagedata.MONOCOLOR_BITMAP)
示例13: main
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def main():
epd = epd4in2.EPD()
epd.init()
# For simplicity, the arguments are explicit numerical coordinates
image = Image.new('1', (EPD_WIDTH, EPD_HEIGHT), 1) # 1: clear the frame
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 24)
draw.rectangle((0, 6, 400, 30), fill = 0)
draw.text((100, 10), 'e-Paper demo', font = font, fill = 255)
draw.rectangle((200, 80, 360, 280), fill = 0)
draw.arc((240, 80, 380, 220), 0, 360, fill = 255)
draw.rectangle((0, 80, 160, 280), fill = 255)
draw.arc((40, 80, 180, 220), 0, 360, fill = 0)
epd.display_frame(epd.get_frame_buffer(image))
image = Image.open('monocolor.bmp')
epd.display_frame(epd.get_frame_buffer(image))
# You can get frame buffer from an image or import the buffer directly:
#epd.display_frame(imagedata.MONOCOLOR_BITMAP)
示例14: main
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def main():
epd = epd7in5b.EPD()
epd.init()
# For simplicity, the arguments are explicit numerical coordinates
image = Image.new('L', (EPD_WIDTH, EPD_HEIGHT), 255) # 255: clear the frame
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 24)
draw.rectangle((0, 6, 640, 40), fill = 127)
draw.text((200, 10), 'e-Paper demo', font = font, fill = 255)
draw.rectangle((200, 80, 600, 280), fill = 127)
draw.chord((240, 120, 580, 220), 0, 360, fill = 255)
draw.rectangle((20, 80, 160, 280), fill = 0)
draw.chord((40, 80, 180, 220), 0, 360, fill = 127)
epd.display_frame(epd.get_frame_buffer(image))
image = Image.open('640x384.bmp')
epd.display_frame(epd.get_frame_buffer(image))
# You can get frame buffer from an image or import the buffer directly:
#epd.display_frame(imagedata.MONOCOLOR_BITMAP)
示例15: main
# 需要导入模块: from PIL import ImageFont [as 别名]
# 或者: from PIL.ImageFont import truetype [as 别名]
def main():
epd = epd2in7.EPD()
epd.init()
# For simplicity, the arguments are explicit numerical coordinates
image = Image.new('1', (epd2in7.EPD_WIDTH, epd2in7.EPD_HEIGHT), 255) # 255: clear the image with white
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 18)
draw.text((20, 50), 'e-Paper demo', font = font, fill = 0)
draw.rectangle((0, 76, 176, 96), fill = 0)
draw.text((18, 80), 'Hello world!', font = font, fill = 255)
draw.line((10, 130, 10, 180), fill = 0)
draw.line((10, 130, 50, 130), fill = 0)
draw.line((50, 130, 50, 180), fill = 0)
draw.line((10, 180, 50, 180), fill = 0)
draw.line((10, 130, 50, 180), fill = 0)
draw.line((50, 130, 10, 180), fill = 0)
draw.arc((90, 190, 150, 250), 0, 360, fill = 0)
draw.chord((90, 120, 150, 180), 0, 360, fill = 0)
draw.rectangle((10, 200, 50, 250), fill = 0)
epd.display_frame(epd.get_frame_buffer(image))
# display images
epd.display_frame(epd.get_frame_buffer(Image.open('monocolor.bmp')))