1
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
0

1 Answer 1

4

Read the docs. It clearly says:

PIL.Image.open(fp, mode='r')

Opens and identifies the given image file.
Returns: An Image object.

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)
Sign up to request clarification or add additional context in comments.

Comments

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.