Pillow: Image.core.fill('L',size,0) expects an int

Created on 12 Jun 2015  Â·  20Comments  Â·  Source: python-pillow/Pillow

this all started over trying to figure out a method to pass unicode font data to OpenGL (no more boxes)
(I'm trying to display this character: ↻ )

I found a nice little hacky method here:
http://pillow-wiredfool.readthedocs.org/en/latest/_modules/PIL/ImageFont.html
after making the proper adjustments to the code in my result, I got my code to look like this:

from PIL import _imagingft
from PIL import Image

def _CreateCharacter(s,F): #(string, Font)
    data_w, data_h = F.getsize(s)

    glyph = Image.core.fill('L', (data_w, data_h), 0)
    F.render(s, glyph.id, mode=="1")

    print glyph.getpixel((0,0)) # testing

I'm trying to set texture images under Luminance-Alpha so I can color them later

but when I start up my program, here's my output:

...
  File "Z:\media\tcll\copy\Tcll\UMC_workspace\UMC_v3.0a\data\GUI.py", line 2451, in _CreateCharacter
    glyph = Image.core.fill('L', (data_w, data_h), 0)
TypeError: an integer is required
Bug

Most helpful comment

I see no problem with that :)
if it ever happens again, I'll create a new issue and refer this one.

All 20 comments

Is this a bug or do you mean to be asking a question on Stack Overflow? Please re-open if you are reporting a Pillow issue. Thanks

it's a bug report.
I can't ask Q's on SO, so I don't even bother to think about it.

I believe the format 'L' is expected to be an int

not sure

whatever it is, Pillow's expecting a wrong argument type when even the PIL src uses that code.

Thanks

The nice little hacky method you found here http://pillow-wiredfool.readthedocs.org/en/latest/_modules/PIL/ImageFont.html is in fact part of Pillow, and you can use Pillow directly.

Pillow and PIL are basically the same thing: Pillow can be thought of as an updated version of PIL.

This doesn't address the bug, but here's an example to create an image with "↻":

from __future__ import unicode_literals
from PIL import Image, ImageDraw, ImageFont

FONT_PATH = "Tests/fonts/FreeMono.ttf"
FONT_SIZE = 100
TEXT = "↻"
im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
draw.text((10, 10), TEXT, font=ttf)
im.save("out.png")

That creates this:
out.png

thanks, but I'm trying to create a GL display-list texture, not save to a PNG ;)
I know DLs are deprecated, the interface I'm using them on is scrapped, but the new interface that uses shaders will take a while to work on due to alot more new features.
so I'm working on the scrapped interface just to get a release out with some ported stuff from the new interface.

and yea, PIL isn't maintained anymore so I know Pillow is basically the new PIL ;)

anyways, from some edging by my friend, I've moved over to freetype-py and kept pillow local to my image I/O script for common formats, so I actually won't be able to test this anymore... heh

anyways, I'll stick around for the update ;)

In Image.core.fill(mode, size, color), mode is a string and size is a tuple of integers:
https://github.com/python-pillow/Pillow/blob/master/_imaging.c#L573

You can see it being called with "L" and "P" string values here:
https://github.com/python-pillow/Pillow/blob/master/PIL/ImageFont.py#L162-164
https://github.com/python-pillow/Pillow/blob/master/PIL/GifImagePlugin.py#L250-251

So "L" should be fine.

What are your values for data_w, data_h? Are they integers or floats or something else?

data_w, data_h = F.getsize(s)

that should always return a tuple of ints right??

EDIT:
F is an ImageFont object (I think)
(I don't have the reference to see if the object is the child of ImageFont, but it's what you get the glyphs from) :P

s is basically any chr(), and that includes the unicode '↻'

What is F exactly?

Please can you have your code print out data_w, data_h to be sure?

print(data_w, data_h)
print(type(data_w), (data_h))
print(F)
print(type(F))

F is the font created with pillow
again, I don't have a reference anymore... heh

If we can reproduce a bug, we can look into fixing it.

Ideally, we'd like to be able to reproduce it using some minimal, self-contained code that have minimal dependencies.

Edit: please can you create a minimal code example to reproduce it?

yea, just pillow, from your code above, and as I've stated:
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

I'm not sure if F is ttf or a child object of ttf, but whatever it is, you can call .getsize( chr() ) from it.
so F.getsize( chr() ) if you can ttf.getsize( chr() ), otherwize F is the child object of ttf with that attribute.

This code example works for me:

from PIL import _imagingft
from PIL import Image, ImageFont

FONT_PATH = "Tests/fonts/FreeMono.ttf"
FONT_SIZE = 100
TEXT = "↻"

ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

data_w, data_h = ttf.getsize(TEXT)

print(data_w, data_h)
print(type(data_w), type(data_h))
print(ttf)
print(type(ttf))

glyph = Image.core.fill('L', (data_w, data_h), 0)

print(glyph)
print(type(glyph))

Output:

(180, 88)
(<type 'int'>, <type 'int'>)
<PIL.ImageFont.FreeTypeFont object at 0x10dfa0250>
<class 'PIL.ImageFont.FreeTypeFont'>
<ImagingCore object at 0x10db90110>
<type 'ImagingCore'>

hmm... maybe it's because of how I'm loading PIL >_>

>>> import sys
>>> sys.path.append('Z:/media/tcll/copy/Tcll/UMC_workspace/UMC_v3.0a/ext/Pillow-2.6.1-py2.7-win32.egg')
>>> import PIL
>>> from PIL import _imagingft

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    from PIL import _imagingft
  File "Z:\media\tcll\copy\Tcll\UMC_workspace\UMC_v3.0a\ext\Pillow-2.6.1-py2.7-win32.egg\PIL\_imagingft.py", line 7, in <module>
  File "Z:\media\tcll\copy\Tcll\UMC_workspace\UMC_v3.0a\ext\Pillow-2.6.1-py2.7-win32.egg\PIL\_imagingft.py", line 3, in __bootstrap__
ImportError: No module named pkg_resources

oddly enough import PIL._imagingft actually works in my program (where I'm loading the egg from)

oh woops, didn't realize I did't have pkg_resources.py installed on my test interpreter, I'll get back to you on the load issue :P

ok, it seems to be working for me now in IDLE >.>
strange...

>>> from PIL import _imagingft
>>> from PIL import Image, ImageFont
>>> FONT_PATH = 'Z:/media/tcll/copy/Tcll/UMC_workspace/UMC_v3.0a/fonts/tahoma.ttf'
>>> FONT_SIZE = 100
>>> TEXT = "↻"
Unsupported characters in input
TEXT = u"↻"
Unsupported characters in input
TEXT = "c"
>>> ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
>>> data_w, data_h = ttf.getsize(TEXT)
>>> glyph = Image.core.fill('L', (data_w, data_h), 0)
>>> 

I wonder if it's because of my interpreter being Portable Python for my program >_>
my test interpreter isn't portable

EDIT:
lemme try my pillow lib for my program, maybe that'll cause it :/

EDIT2: nope...
I can't seem to duplicate the issue... weird

I'll figure it out... it's probably a priority issue or something...
at least I'm not the only one affected by it... a quick google search on the error will show a few other people who've gotten it, one in particular on SO which caught my interest a while back...

I'll continue looking into it tomorrow, I've gotten it once and was very frustrated trying to get rid of it,
I'll make sure to get it again and see exactly what happens... heh

Any updates on this? Would you be able to provide links to the StackOverflow reports of the bug, so that we can try and replicate it from those?

sadly no, I havn't really been looking into it recently, I'm rather a little overworked with a few other projects...

but as for a few of the pages I'd visited, it seems to be that they'd changed the image from 'L' to 'RGB' before calling fill() which fixed it for them:

http://stackoverflow.com/questions/20658265/pil-imagedraw-draw-text-fill-attribute-raise-typeerror-an-integer-is-required

http://www.gossamer-threads.com/lists/python/python/1189387

this was just a quick google search for links I'd already visited, so there's likely a few more I'm missing

Hmm. Unfortunately, those links don't provide enough information to try and reproduce the problem.

Looking one of them though, I'm starting to wonder if this is related to a recent PR I've made, #1387. If you're ever able to replicate this again, I'd be interested to know if that helps.

PR https://github.com/python-pillow/Pillow/pull/1387 has been merged. Can this be closed?

I see no problem with that :)
if it ever happens again, I'll create a new issue and refer this one.

Was this page helpful?
0 / 5 - 0 ratings