PIL-TypeError:src不是numpy数组,也不是标量 [英] PIL - TypeError: src is not a numpy array, neither a scalar
本文介绍了PIL-TypeError:src不是numpy数组,也不是标量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
from PIL import Image
import pytesseract
import argparse
import cv2
import os
image = Image.open("C:/Users/NB/Desktop/Scan/Arti818.jpg")
#image = "C:/Users/NB/Desktop/Scan/Arti818.jpg"
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# check to see if we should apply thresholding to preprocess the
# image
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# make a check to see if median blurring should be done to remove
# noise
# write the grayscale image to disk as a temporary file so we can
# apply OCR to it
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
# load the image as a PIL/Pillow image, apply OCR, and then delete
# the temporary file
text = pytesseract.image_to_string(Image.open(filename))
os.remove(filename)
print(text)
# show the output images
cv2.imshow("Image", image)
cv2.imshow("Output", gray)
cv2.waitKey(0)
这是我的代码,出现以下错误:
This is my code and I am getting following error:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
TypeError: src is not a numpy array, neither a scalar
推荐答案
阅读文档.它清楚地说:
PIL
.Image
.open
(fp, mode='r'
)打开并标识给定的图像文件.
返回:一个Image
对象.
PIL
.Image
.open
(fp, mode='r'
)Opens and identifies the given image file.
Returns: AnImage
object.
返回的对象属于Image
类型,而不是numpy.ndarray
.如果需要数组,请将image
转换为一个:
The object returned is of Image
type, not a numpy.ndarray
. If you want an array, convert image
to one:
gray = cv2.cvtColor(np.asarray(image), cv2.COLOR_BGR2GRAY)
这篇关于PIL-TypeError:src不是numpy数组,也不是标量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文