PIL ImageDraw.Draw.text填充属性引发TypeError:需要一个整数

时间:2022-11-20 00:22:15

I'm trying to write text on an image of a QR-Code, but I can't find a way to write in another colour than white (which is pretty useless for a QR-Code).

我正在尝试在QR码的图像上写文字,但我找不到用白色写的另一种颜色的方法(这对于QR码来说是无用的)。

So here is the error page I get (I'm using Django too) :

所以这是我得到的错误页面(我也使用Django):

TypeError at /pret/qrcode/

an integer is required

Request Method:     GET
Request URL:    http://127.0.0.1:8000/pret/qrcode/
Django Version:     1.6.1
Exception Type:     TypeError
Exception Value:    

an integer is required

Exception Location:     /usr/lib/python2.7/dist-packages/PIL/ImageDraw.py in _getink, line 146
Python Executable:  /usr/bin/python
Python Version:     2.7.3

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/arthur/workspace/PretD/pret/views.py" in qrcodee
  160.         draw.text((0, 0),"This is a test",(255,255,0),font=font)
File "/usr/lib/python2.7/dist-packages/PIL/ImageDraw.py" in text
  256.         ink, fill = self._getink(fill)
File "/usr/lib/python2.7/dist-packages/PIL/ImageDraw.py" in _getink
  146.                 ink = self.draw.draw_ink(ink, self.mode)

Exception Type: TypeError at /pret/qrcode/
Exception Value: an integer is required

In my view I have:

在我看来,我有:

    foo = qrcode.make(request.user.email)
    foo.format = "PNG"
    foo.save('pret/static/media/qrcode.png')

    font = ImageFont.truetype("pret/static/DejaVuSans.ttf", 20)

    img=Image.open("pret/static/media/qrcode.png")
    draw = ImageDraw.Draw(img)
    draw.text((0, 0),"String test",(255,255,0),font=font)
    draw = ImageDraw.Draw(img)
    del draw
    img.save('pret/static/media/qrcode.png')

with these imports:

使用这些进口:

import ImageDraw
import ImageFont
import Image

NB: Python couldn't find "PIL" when I tried to write

注意:当我尝试编写时,Python无法找到“PIL”

import PIL
from PIL import Image, ImageFond, ImageDraw

but it worked without mentioning it, I guess it's already included in Django by default. Plus, we can see in the traceback that Python does indeed look into /dist-packages/PIL

但它没有提到它,但我猜它默认已包含在Django中。另外,我们可以在回溯中看到Python确实关注/ dist-packages / PIL

Thank you for your help.

感谢您的帮助。

1 个解决方案

#1


6  

Does your image have the right mode? I guess the current mode is 'wrong'. An image should be in RGB mode before it accepts a RGB color tuple.

您的图像是否具有正确的模式?我猜当前的模式是“错误的”。在接受RGB颜色元组之前,图像应处于RGB模式。

Read about mode: http://effbot.org/imagingbook/concepts.htm#mode

阅读模式:http://effbot.org/imagingbook/concepts.htm#mode

Convert the image to RGB before writing the yellow text:

在写入黄色文本之前将图像转换为RGB:

img=Image.open("foo.png")

# Convert to RGB mode
if img.mode != "RGB":
    img = img.convert("RGB")

draw = ImageDraw.Draw(img)
draw.text((0, 0), "Foo", (255, 0, 0), font=font)
img.save('foo.png')

This import fails because of misspelled ImageFond. Should read:

由于拼写错误的ImageFond导致此导入失败。应该读:

from PIL import Image, ImageFont, ImageDraw

If you still have import problems try Pillow. Pillow is a better-packaged version of PIL.

如果您仍有进口问题,请尝试Pillow。 Pillow是PIL的更好包装版本。

#1


6  

Does your image have the right mode? I guess the current mode is 'wrong'. An image should be in RGB mode before it accepts a RGB color tuple.

您的图像是否具有正确的模式?我猜当前的模式是“错误的”。在接受RGB颜色元组之前,图像应处于RGB模式。

Read about mode: http://effbot.org/imagingbook/concepts.htm#mode

阅读模式:http://effbot.org/imagingbook/concepts.htm#mode

Convert the image to RGB before writing the yellow text:

在写入黄色文本之前将图像转换为RGB:

img=Image.open("foo.png")

# Convert to RGB mode
if img.mode != "RGB":
    img = img.convert("RGB")

draw = ImageDraw.Draw(img)
draw.text((0, 0), "Foo", (255, 0, 0), font=font)
img.save('foo.png')

This import fails because of misspelled ImageFond. Should read:

由于拼写错误的ImageFond导致此导入失败。应该读:

from PIL import Image, ImageFont, ImageDraw

If you still have import problems try Pillow. Pillow is a better-packaged version of PIL.

如果您仍有进口问题,请尝试Pillow。 Pillow是PIL的更好包装版本。