问题遇到的现象和发生背景
> D:\PYTHON\python.exe D:/PYTHON/practise/test/p图.py
Traceback (most recent call last):
File "D:/PYTHON/practise/test/p图.py", line 15, in <module>
img_pil = Image.fromarray(bk_img)
File "D:\PYTHON\lib\site-packages\PIL\Image.py", line 2818, in fromarray
arr = obj.__array_interface__
AttributeError: 'NoneType' object has no attribute '__array_interface__'
Process finished with exit code 1
问题相关代码,请勿粘贴截图
import cv2
from PIL import ImageFont, ImageDraw, Image
import numpy as np
img=r'C:\Users\锺少\Desktop\Screenshot_20200611-123839.jpg'
bk_img = cv2.imread(img)
#设置需要显示的字体
fontpath = "C:\Windows\Fonts\simhei.ttf"
font = ImageFont.truetype(fontpath,32)
img_pil = Image.fromarray(bk_img)
draw = ImageDraw.Draw(img_pil)
#绘制文字信息
draw.text((100, 300), "Hello World", font = font, fill = (255, 255, 255))
draw.text((100, 350), "你好", font = font, fill = (255, 255, 255))
bk_img = np.array(img_pil)
cv2.imshow("add_text",bk_img)
cv2.waitKey()
cv2.imwrite("add_text.jpg",bk_img)