4

Okay so i'm trying to add an indian/nepali font for a gesture recognition system and i'm struggling a bit with the Pillow library. I followed the documentation for pillow and i'm getting an AttributeError.

My Code:

def put_splitted_text_in_blackboard(blackboard, splitted_text):
draw = ImageDraw.Draw(blackboard)
for text in splitted_text:
    fonts = ImageFont.truetype("preeti.TTF", 50)
    draw.text((10, 25), text, font=fonts)

It's giving me this error:

Traceback (most recent call last):
File "C:\Program Files\Python36\lib\site-packages\PIL\ImageDraw.py", line 
289, in Draw
return im.getdraw(mode)
AttributeError: 'numpy.ndarray' object has no attribute 'getdraw'

The blackboard has been defined here:

def recognize()
blackboard = np.zeros((480, 640, 3), dtype=np.uint8)
    splitted_text = split_sentence(text, 2)
    put_splitted_text_in_blackboard(blackboard, splitted_text)
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    res = np.hstack((img, blackboard))
    cv2.imshow("Recognizing gesture", res)
    cv2.imshow("thresh", thresh)
    if cv2.waitKey(1) == ord('q'):
        break

Anyone know where it went wrong?

5
  • 1
    The first arg to ImageDraw.Draw should be a PIL Image object, but you passed it a Numpy array. Commented Jun 17, 2018 at 10:45
  • okay yeah i got it, my bad but if i want to display characters on a blank screen, how do i pass a PIL Image to do so? Commented Jun 17, 2018 at 10:52
  • You can use PIL Image.new to create a blank image of whatever size you want, and a Image object has a .show method you can use to display it. There are fast methods to convert a PIL Image to a Numpy array, and vice versa, in case you need Numpy to do some processing that's not convenient in PIL. It's probably not a good idea to mix PIL & cv2 in the one program. It won't hurt anything, but it's a recipe for confusion. ;) For example, PIL uses the RGB convention, but cv2 uses BGR. Commented Jun 17, 2018 at 10:57
  • Like if i was just to use one of the pre-defined fonts in OpenCV i'd use: cv2.putText(blackboard, text, (4, 10), cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 2, (255, 255, 255)) i'm still very much confused as to how PIL works. Commented Jun 17, 2018 at 11:00
  • In PIL you use ImageFont. It's probably a good idea at this stage to spend a bit of time browsing the PIL docs to get an idea of the tools that it offers. Commented Jun 17, 2018 at 11:02

1 Answer 1

7

What you need to do is change this line:

draw = ImageDraw.Draw(blackboard)

to

draw = ImageDraw.Draw(Image.fromarray(blackboard))

Which give ImageDraw an Image which it can understand not a numpy array which it can't.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry typo: should be fromarray!!
Thank you so much. That did the trick. Side question: any idea as to why openCV doesn't support international fonts? Kinda weird that it only supports one font family.
If you could accept my answer - I will edit to remove typo. Don't know OpenCV very well at all so no I doon't, maybe cotact development team via github?
Done. Thanks for your help Paula.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.