在python中将字符串转换为图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17856242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:19:39  来源:igfitidea点击:

Convert string to image in python

pythonpython-imaging-library

提问by Marco-

I started to learn python a week ago and want to write a small programm that converts a email to a image (.png) so that it can be shared on forums without risking to get lots of spam mails.

一周前我开始学习 python,想编写一个小程序,将电子邮件转换为图像 (.png),以便它可以在论坛上共享,而不会冒收到大量垃圾邮件的风险。

It seems like the python standard libary doesn't contain a module that can do that but i`ve found out that there's a PIL module for it (PIL.ImageDraw).

似乎python标准库不包含可以做到这一点的模块,但我发现它有一个PIL模块(PIL.ImageDraw)。

My problem is that i can't seem to get it working.

我的问题是我似乎无法让它工作。

So basically my questions are:

所以基本上我的问题是:

  1. How to draw a text onto a image.
  2. How to create a blank (white) image
  3. Is there a way to do this without actually creating a file so that i can show it in a GUI before saving it?
  1. 如何在图像上绘制文本。
  2. 如何创建空白(白色)图像
  3. 有没有办法在不实际创建文件的情况下做到这一点,以便我可以在保存之前在 GUI 中显示它?

Thanks for your help :)

谢谢你的帮助 :)

Current Code:

当前代码:

import Image
import ImageDraw
import ImageFont

def getSize(txt, font):
    testImg = Image.new('RGB', (1, 1))
    testDraw = ImageDraw.Draw(testImg)
    return testDraw.textsize(txt, font)

if __name__ == '__main__':

    fontname = "Arial.ttf"
    fontsize = 11   
    text = "[email protected]"

    colorText = "black"
    colorOutline = "red"
    colorBackground = "white"


    font = ImageFont.truetype(fontname, fontsize)
    width, height = getSize(text, font)
    img = Image.new('RGB', (width+4, height+4), colorBackground)
    d = ImageDraw.Draw(img)
    d.text((2, height/2), text, fill=colorText, font=font)
    d.rectangle((0, 0, width+3, height+3), outline=colorOutline)

    img.save("D:/image.png")

采纳答案by Jan Spurny

  1. use ImageDraw.text- but it doesn't do any formating, it just prints string at the given location

    img = Image.new('RGB', (200, 100))
    d = ImageDraw.Draw(img)
    d.text((20, 20), 'Hello', fill=(255, 0, 0))
    

    to find out the text size:

    text_width, text_height = d.textsize('Hello')
    
  2. When creating image, add an aditional argument with the required color (white):

    img = Image.new('RGB', (200, 100), (255, 255, 255))
    
  3. until you save the image with Image.savemethod, there would be no file. Then it's only a matter of a proper transformation to put it into your GUI's format for display. This can be done by encoding the image into an in-memory image file:

    import cStringIO
    s = cStringIO.StringIO()
    img.save(s, 'png')
    in_memory_file = s.getvalue()
    

    or if you use python3:

    import io
    s = io.BytesIO()
    img.save(s, 'png')
    in_memory_file = s.getvalue()
    

    this can be then send to GUI. Or you can send direct raw bitmap data:

    raw_img_data = img.tostring()
    
  1. 使用ImageDraw.text- 但它不做任何格式化,它只是在给定的位置打印字符串

    img = Image.new('RGB', (200, 100))
    d = ImageDraw.Draw(img)
    d.text((20, 20), 'Hello', fill=(255, 0, 0))
    

    找出文字大小:

    text_width, text_height = d.textsize('Hello')
    
  2. 创建图像时,添加具有所需颜色(白色)的附加参数:

    img = Image.new('RGB', (200, 100), (255, 255, 255))
    
  3. 直到你用Image.save方法保存图像,才会有文件。然后,只需进行适当的转换即可将其放入您的 GUI 格式中进行显示。这可以通过将图像编码到内存中的图像文件来完成:

    import cStringIO
    s = cStringIO.StringIO()
    img.save(s, 'png')
    in_memory_file = s.getvalue()
    

    或者如果您使用 python3:

    import io
    s = io.BytesIO()
    img.save(s, 'png')
    in_memory_file = s.getvalue()
    

    然后可以将其发送到 GUI。或者您可以直接发送原始位图数据:

    raw_img_data = img.tostring()
    

回答by Uwe_98

The first 3 lines are not complete, when I'm not wrong. The correct code would be:

前 3 行不完整,当我没有错的时候。正确的代码是:

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont