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?
ImageDraw.Drawshould be a PILImageobject, but you passed it a Numpy array.Image.newto create a blank image of whatever size you want, and a Image object has a.showmethod 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.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.