PIL ImageDraw.Draw.text attribute fill raise TypeError: integer required

I am trying to write text on the image of a QR code, but I cannot find a way to write in a different color than white (which is pretty useless for a QR code).

So here is the error page I get (I use Django too):

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 opinion, 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 

Note: Python could not find "PIL" when I tried to write

 import PIL from PIL import Image, ImageFond, ImageDraw 

but it worked without mentioning it, I think it is already included in Django by default. In addition, we can see in the trace that Python is really looking for / dist -packages / PIL

Thank you for your help.

+6
source share
1 answer

Does your image have the correct mode? I assume that the current mode is "wrong." The image must be in RGB mode before it accepts the RGB color set.

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

Convert the image to RGB before writing yellow text:

 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 is not performed due to an incorrectly written ImageFond. It should be read:

 from PIL import Image, ImageFont, ImageDraw 

If you still have import issues, try Pillow. Pillow is a better packaged version of PIL.

+6
source

All Articles