簡體   English   中英

可以將numpy數組顯示/集成到PIL嗎?

[英]Possible to display/integrate numpy array with PIL?

我已經在這里問過兩次了,但沒有得到回應。 那么第三次幸運嗎?

如何使用PIL顯示Numpy數組? 我正在嘗試將Times New Roman字體與PIL一起使用,並且需要在使用numpy數組的空白屏幕上顯示字符。

如果我使用內置的cv2.putText函數,則它的工作原理如下:

cv2.putText(blackboard, text, (4, y), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 255, 255))

輸出: output1

但是由於必須使用Times New Roman,因此我使用了PIL庫。

碼:

def put_splitted_text_in_blackboard(blackboard, splitted_text):
y = 200
blackb = Image.fromarray(blackboard)
draw = ImageDraw.Draw(blackb)
for text in splitted_text:
    fonts = ImageFont.truetype('Times New Roman.ttf', 50, encoding='unic')
    draw.text((4, y), text, font=fonts, fill=(255, 255, 255))
    y += 50

numpy數組在identify()中定義如下:

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


keras_predict(model, np.zeros((50, 50), dtype=np.uint8))
recognize()

當我運行此模塊時,程序執行正常,但在空白屏幕上看不到文本。

輸出: Output2

有什么幫助嗎?

您正在從blackboard numpy數組創建一個PIL圖像並對其進行繪制,但沒有將結果寫回到Numpy數組。 嘗試在put_splitted_text_in_blackboard函數的末尾添加return np.array(blackb) ,並在程序的其余部分中使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM