image.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:reddit-iambic-pentameter 作者: pmichel31415 项目源码 文件源码
def make_image(text, fontsize=45, output_file='tmp.png', fontname='HamletOrNot.ttf'):
    """Make an image out of a poem"""
    # Get font
    font = ImageFont.truetype(fontname, fontsize * factor)
    # Compute height
    num_lines = (1 + text.strip().count('\n'))
    height = num_lines * font.getsize(text[:10])[1] / factor + 2 * pad
    # Compute width
    font_length = max(font.getsize(line)[0] for line in text.split('\n'))
    width = font_length / factor + 2 * pad
    # Create big image and draw text
    image = Image.new("RGBA", (width * factor, height * factor), (241, 241, 212))
    draw = ImageDraw.Draw(image)
    draw.text((pad * factor, pad * factor), text, (0, 0, 0), font=font)
    # Resize with antialiasing
    img_resized = image.resize((width, height), Image.ANTIALIAS)
    # Save to file
    img_resized.save(output_file)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号