当前位置: 首页>>代码示例>>Python>>正文


Python Draw.textsize方法代码示例

本文整理汇总了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 )
开发者ID:solomongarber,项目名称:texture_sampler,代码行数:27,代码来源:JBhelpers.py

示例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
开发者ID:xxguo,项目名称:leopard,代码行数:28,代码来源:captcha.py

示例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
开发者ID:Chenboxi2015,项目名称:iHome,代码行数:32,代码来源:captcha.py

示例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
开发者ID:redteamcaliber,项目名称:deep-learning-course,代码行数:43,代码来源:handwritting.py

示例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
开发者ID:mailgyc,项目名称:doudizhu,代码行数:25,代码来源:captcha.py


注:本文中的PIL.ImageDraw.Draw.textsize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。