Python Script, tesseract. No error but no output - python

I am trying to use tesseract to read text from an image, i have installed tesseract using home brew but i get no output. Here is my code;
import pytesseract
from PIL import Image
value = Image.open('/Users/director/Desktop/databoy/easytt.jpeg')
text = pytesseract.image_to_string(value, lang="eng")
print(text)
I don't get any errors, and it does not print out anything, i just get:
Process finished with exit code 0

Related

Why is Tesseract unable to detect the single digit in that image?

I have this image, and I'm trying to read it with Tesseract:
My code is like that:
pytesseract.image_to_string(im)
But, what I get is only LOW: 56. So, Tesseract is unable to read the 1 in the first line. I've tried to specify also a whitelist of only digits like
pytesseract.image_to_string(im, config="tessedit_char_whitelist=0123456789.")
and to process the image with an erosion but nothing works. Any suggestions?
Improving the quality of the output is your "holy scripture" when working with Tesseract. Especially, the page segmentation method should always be explicitly set. Here (as most of the times), I'd opt for --psm 6:
Assume a single uniform block of text.
Even without further preprocessing of your image, you already get the desired result:
import cv2
import pytesseract
image = cv2.imread('gBrcd.png')
text = pytesseract.image_to_string(image, config='--psm 6')
print(text.replace('\f', ''))
# 1
# LOW: 56
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.19041-SP0
Python: 3.9.1
PyCharm: 2021.1.1
OpenCV: 4.5.2
pytesseract: 5.0.0-alpha.20201127
----------------------------------------

How can I convert a .ps file into a .png file?

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)

pyTesseract not outputing text from image

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.

Python qrcode module not being saved

If I run this:
import qrcode
img = qrcode.make('Some data here')
I don't know where it saves the actual image. I've tried CD'ing to a path and run the Python code, I have had the script in a directory. But when running the code above it doesn't create a QR image file in the directory. Where is it saved?!
I've tried running qr "Some data here" > test.png from the command-line which works perfectly. But not the module itself for some reason.
The make function doesn't output an image. For that you have to do something like this after running that function:
img = qr.make_image(fill_color="black", back_color="white")
Read the docs.

search for latest image inside a folder, and save it as a png file with python

I am new to python. I want to do some image analysis using the python on Raspberry Pi.
I am streaming images using motion to a folder. In that folder, I want to find the latest image at any point of time. Then I would like to apply my image analysis command on that one image and export the results to a text file. I have achieved most parts of it. But struck at one small bit.
import subprocess
import os
import glob
newest = max(glob.iglob('*.[p][n][g]'), key=os.path.getctime)
print(newest)
cmd = "sudo ./deepbelief lena.png > try5.txt"
proc = subprocess.Popen(cmd, shell=True)
In the above deepbelief is my image analysis program. Now the problem is how can I feed my newest image as input to the command ./deepbelief.
Also, if possible, can I save newest image as an png file for later use?
Many thanks in advance!
You can use string formatting:
cmd = "sudo ./deepbelief {} > try5.txt".format(newest)
The {} will be whatever the value of newest is, so for example if newest is foo.png, you're command will be "sudo ./deepbelief foo.png > try5.txt" and so on..

Categories