本文整理汇总了Python中PIL.ImageDraw.Draw.textsize方法的典型用法代码示例。如果您正苦于以下问题:Python Draw.textsize方法的具体用法?Python Draw.textsize怎么用?Python Draw.textsize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PIL.ImageDraw.Draw
的用法示例。
在下文中一共展示了Draw.textsize方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: draw_text
# 需要导入模块: from PIL.ImageDraw import Draw [as 别名]
# 或者: from PIL.ImageDraw.Draw import textsize [as 别名]
def draw_text( data, text, color = 255, pos = 'lr' ):
from PIL.Image import fromarray
from PIL.ImageDraw import Draw
from PIL import ImageFont
from numpy import asarray
font = ImageFont.load_default()
image = fromarray( data )
draw = Draw( image )
w, h = draw.textsize( text, font = font )
position = {
'ul': lambda iw, ih, tw, th: ( 2, 0 ),
'ur': lambda iw, ih, tw, th: ( iw - tw - 2, 0 ),
'll': lambda iw, ih, tw, th: ( 2, ih - th ),
'lr': lambda iw, ih, tw, th: ( iw - tw - 2, ih - th ),
}
pos = position[ pos ]( data.shape[ 1 ], data.shape[ 0 ], w, h )
draw.text( pos, text, fill = color, font = font )
del draw
return asarray( image )
示例2: drawer
# 需要导入模块: from PIL.ImageDraw import Draw [as 别名]
# 或者: from PIL.ImageDraw.Draw import textsize [as 别名]
def drawer(image, text):
draw = Draw(image)
char_images = []
for c in text:
font = random.choice(fonts)
c_width, c_height = draw.textsize(c, font=font)
c_height *= 2
char_image = Image.new('RGB', (c_width, c_height), (0, 0, 0))
char_draw = Draw(char_image)
char_draw.text((0, 0), c, font=font, fill=color())
char_image = char_image.crop(char_image.getbbox())
for drawing in drawings:
char_image = drawing(char_image)
char_images.append(char_image)
width, height = image.size
offset = int((width - sum(int(i.size[0] * squeeze_factor)
for i in char_images[:-1])
- char_images[-1].size[0]) / 2)
for char_image in char_images:
c_width, c_height = char_image.size
mask = char_image.convert('L').point(lambda i: i * 1.97)
image.paste(char_image,
(offset, int((height - c_height) / 2)),
mask)
offset += int(c_width * squeeze_factor)
return image
示例3: text
# 需要导入模块: from PIL.ImageDraw import Draw [as 别名]
# 或者: from PIL.ImageDraw.Draw import textsize [as 别名]
def text(self, image, fonts, font_sizes=None, drawings=None, squeeze_factor=0.75, color=None):
color = color if color else self._color
fonts = tuple([truetype(name, size)
for name in fonts
for size in font_sizes or (65, 70, 75)])
draw = Draw(image)
char_images = []
for c in self._text:
font = random.choice(fonts)
c_width, c_height = draw.textsize(c, font=font)
char_image = Image.new('RGB', (c_width, c_height), (0, 0, 0))
char_draw = Draw(char_image)
char_draw.text((0, 0), c, font=font, fill=color)
char_image = char_image.crop(char_image.getbbox())
for drawing in drawings:
d = getattr(self, drawing)
char_image = d(char_image)
char_images.append(char_image)
width, height = image.size
offset = int((width - sum(int(i.size[0] * squeeze_factor)
for i in char_images[:-1]) -
char_images[-1].size[0]) / 2)
for char_image in char_images:
c_width, c_height = char_image.size
mask = char_image.convert('L').point(lambda i: i * 1.97)
image.paste(char_image,
(offset, int((height - c_height) / 2)),
mask)
offset += int(c_width * squeeze_factor)
return image
示例4: decompose_word
# 需要导入模块: from PIL.ImageDraw import Draw [as 别名]
# 或者: from PIL.ImageDraw.Draw import textsize [as 别名]
def decompose_word(img, word, font):
x_size = img.size[0]
y_size = img.size[1]
letters = len(word)
array = img_to_array(img)
array.shape = (array.shape[1], array.shape[2])
#print word, img.size
draw = Draw(Image.new("L",(100,100)))
total_font_size = float(draw.textsize(word, font)[0])
data = []
start = 0
for letter in word: # range(0,x_size, x_size / len(word)):
#print i*word_size,(i+1)*word_size, array.shape
letter_size = draw.textsize(letter, font)[0]
size = int(x_size*letter_size/total_font_size)
middle = int((start + size) / 2)
a = max(0,middle - 64)
b = min(x_size, middle + 64)
#print acc_size, acc_size+x
img = array[:,a:b]
if middle - 64 < 0:
padding = 255*np.ones((y_size, 64 - middle))
print padding.shape, img.shape
img = np.hstack([padding,img])
if middle + 64 > x_size:
padding = 255*np.ones((y_size, (middle + 64) - x_size))
print padding.shape, img.shape
img = np.hstack([img,padding])
data.append((img,letter))
start = start + size
#acc_size = acc_size + x
return data
示例5: draw_text
# 需要导入模块: from PIL.ImageDraw import Draw [as 别名]
# 或者: from PIL.ImageDraw.Draw import textsize [as 别名]
def draw_text(image, text, font_size):
font = truetype(join(current_dir, 'static/font.ttf'), font_size)
color = '#5C87B2'
draw = Draw(image)
char_images = []
for ch in text:
c_width, c_height = draw.textsize(ch, font=font)
char_image = Image.new('RGB', (c_width, c_height), (0, 0, 0))
char_draw = Draw(char_image)
char_draw.text((0, 0), ch, font=font, fill=color)
char_image = char_image.crop(char_image.getbbox())
char_images.append(char_image)
width, height = image.size
total = len(char_images)
for i, char_image in enumerate(char_images):
c_width, c_height = char_image.size
mask = char_image.convert('L').point(lambda i: i * 1.97)
upper = int((height - c_height) / 2)
left = int((width * (i + 1) / (total + 1)) - c_width / 2)
image.paste(char_image, (left, upper), mask)
return image