maybe someone could help me! When I run the following code
import pytesseract
from pytesseract import image_to_string
from PIL import Image
import PIL
file = Image.open('/usr/local/Cellar/tesseract/4.1.0/share/tessdata/cap.png')
we_will = pytesseract.image_to_string(file)
print(we_will)
all that gets outputed is:
Process finished with exit code 0
which is no help. What am I doing wrong?
Sounds like we_will is the empty string. Try printing repr(we_will) to understand this idea more clearly.
IIRC, PyTesseract does this when it can't figure out what the text is inside the image. You can get the best results for a cropped image with a single-color background.
Related
I have this image and my code to convert it to string is:
import pytesseract
from PIL import Image
print(pytesseract.image_to_string(Image.open("screenshot.png")))
and my ouptut is nothing...
any suggestion?
The string conversion of an image might be very long... Depending on your editor, it may hide it
I need to convert .ps files to .png files as part of an image recognition program I am making. I know I can use Ghostscript or other programs, but could someone give a specific example of how to write something like this:
def ps_to_png(ps_file):
file = ghostscript.read(ps_file)
png_file = ghostscript.save(file, "png")
return png_file
(This code is pseudo code- I want to know how to write something that actually does what this code looks like it will do.)
Thanks in advance! Stack is a great community and I appreciate it.
EDIT (Attempted solutions): When running this line:
os.system("ghostscript file.ps file.png")
I get the following Error:
'ghostscript' is not recognized as an internal or external command, operable program or batch file.
When attempting to use Pillow:
from PIL import Image
def convert_to_png(ps_file):
img = Image.open(ps_file)
img.save("img.png")
I get the following error:
OSError: Unable to locate Ghostscript on paths
You can use Pillow.
from PIL import Image
psimage=Image.open('myImage.ps')
psimage.save('myImage.png')
If you want to wrap it to a function:
from PIL import Image
def convert_to_png(path):
img = Image.open(path)
img.save("img.png")
path='/path_to_your_file'
convert_to_png(path)
import pytesseract
from pdf2image import convert_from_path, convert_from_bytes
import cv2,numpy
def pil_to_cv2(image):
open_cv_image = numpy.array(image)
return open_cv_image[:, :, ::-1].copy()
path='OriginalsFile.pdf'
images = convert_from_path(path)
cv_h=[pil_to_cv2(i) for i in images]
img_header = cv_h[0][:160,:]
#print(pytesseract.image_to_string(Image.open('test.png'))) I only found this in tesseract docs
Hello, is there a way to read the img_header directly using pytesseract without saving it,
pytesseract docs
pytesseract.image_to_string() input format
As documentation explains pytesseract.image_to_string() needs a PIL image as input.
So you can convert your CV image into PIL one easily, like this:
from PIL import Image
... (your code)
print(pytesseract.image_to_string(Image.fromarray(img_header)))
if you really don't want to use PIL!
see:
https://github.com/madmaze/pytesseract/blob/master/src/pytesseract.py
pytesseract is an easy wrapper to run the tesseract command def run_and_get_output() line, you'll see that it saves your image into an temporary file, and then gives the address to the tesseract to run.
hence, you can do the same with opencv, just rewrite the pytesseract only .py file to do it with opencv, although; i don't see any performance improvements whatsoever.
The fromarray function allows you to load the PIL document into tesseract without saving the document to disk, but you should also ensure that you don`t send a list of pil images into tesseract. The convert_from_path function can generate a list of pil images if a pdf document contains multiple pages, therefore you need to send each page into tesseract individually.
import pytesseract
from pdf2image import convert_from_path
import cv2, numpy
def pil_to_cv2(image):
open_cv_image = numpy.array(image)
return open_cv_image[:, :, ::-1].copy()
doc = convert_from_path(path)
for page_number, page_data in enumerate(doc):
cv_h= pil_to_cv2(page_data)
img_header = cv_h[:160,:]
print(f"{page_number} - {pytesseract.image_to_string(Image.fromarray(img_header))}")
I am trying to extract the characters in the x-ray, I have tried using pytesseract to extract but couldn't succeed, I used a canny edge to remove the noise and extract, but still, I am not able to extract the text/chars. Can you please help/guide me to extract the text/chars
Try this tuotrial to locate the text:
https://www.pyimagesearch.com/2018/08/20/opencv-text-detection-east-text-detector/
Then once you locate you can isolate and use tesseract to recognize it.
If it's a DICOM file, you could use gdcm to get the attribute. It's available on python too.
pytesseract should be sufficient, if the file is in 'png' or 'jpg' form.
now suppose image is the name of your image. Please write the below code.
from PIL import Image
from pytesseract import image_to_string
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
im = Image.open('F:/kush/invert.jpg')
pytesseract.image_to_string(im, lang = 'eng')
how to read this png file using tesseract?
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAAA3CAIAAAAQUt0MAAAWXUlEQVR42u2deWAU5d3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jKLE++ubSTtZj6bpEciNIhCIG5QbbWAUySgCgUBw5T8aYmrcaQtzUgAAAABJRU5ErkJggg==
Decoding Captcha isn't easy job, but maybe this example would be helpful.
In order to recognize text from Captcha you need more magic with cv2.
bfile='blablabla' #your base64 image
ffile=bfile.decode('base64')
pl=open('capcha.png','wb')
pl.write(ffile)
pl.close()
import pytesseract
from PIL import Image
img=Image.open('capcha.png')
text = pytesseract.image_to_string(img)
print (text)