i'm trying to write a python script that will take an image as an input and print out whatever is in the image as text to the terminal or a file. i do have python 2.7 and 3.7
i do have PIL and pytesseract install on my Kali linux
but i'm getting this errors
Traceback (most recent call last):
File "imgtotxt.py", line 8, in <module>
img =Image.open("/home/Desktop/ITT/1.jpeg")
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2609, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '/home/Desktop/ITT/1.jpeg'
HERE IS MY CODE
#!/usr/bin/python
from PIL import Image
from pytesseract import image_to_string
img =Image.open("/home/Desktop/ITT/1.jpeg")
text =image_to_string(img)
print (text)
Something is wrong with how you typed the filename.
Try this in your python code:
import os
print(os.listdir("/home/Desktop/ITT/"))
You should see your filename printed. Copy the filename from there instead.
If this fails, go up a directory (eg /home/Desktop) and try that.
Make sure if the file exists at the exact location you specified. The system isn't finding the file. Perhaps it's at /home/YOUR_USER/Desktop/ITT/1.jpeg ?
Put the script in the same folder as is the image, change path to only a name of the image and you will se if something is REALLY wrong.
EDIT:
Try this then:
import cv2
import numpy as np
image = cv2.imread('1.jpeg') # alternativly /home/Desktop/ITT/
img = Image.fromarray(image.astype(np.uint8))
....
Also check if your image is not corrupted. This is pretty strange
Related
I have already read answer of this question Image.open() cannot identify image file - Python?, that question was solved by using from PIL import Image, but my situation is different. I am using image_slicer, and there I am getting these errors:
Traceback (most recent call last):
File "image_slice.py", line 17, in <module>
j=image_slicer.slice('file_name' , n_k)
File "/home/user_name/.local/lib/python3.5/site-
packages/image_slicer/main.py", line 114, in slice
im = Image.open(filename)
File "/home/user_name/.local/lib/python3.5/site-packages/PIL/Image.py", line 2687, in open
% (filename if filename else fp))
OSError: cannot identify image file 'file_name'
The full code is:
import os
from PIL import Image
import image_slicer
import numpy as np
import nibabel as nib
img = nib.load('/home/user_name/volume-20.nii')
img.shape
epi_img_data = img.get_data()
#epi_img_data.shape
n_i, n_j, n_k = epi_img_data.shape
center_i = (n_i - 1) // 2
center_j = (n_j - 1) // 2
center_k = (n_k - 1) // 2
centers = [center_i, center_j, center_k]
print("Co-ordinates in the voxel array: ", centers)
#for i in range(n_k):
j=image_slicer.slice('/home/user_name/volume-20.nii' , n_k)
However nib.load(), works fine, but image_slicer is not working.
All the nii images are 3D images.
Image slicer is not intended for reading nii format. Here is the list of supported formats.
This error also occurs whenever the image file itself is corrupted. I once accidentally was in the process of deleting the subject image, until canceling mid-way through.
TL;DR - open image file to see if it's ok.
I'm trying to use a psm of 0 with pytesseract, but I'm getting an error. My code is:
import pytesseract
from PIL import Image
img = Image.open('pathToImage')
pytesseract.image_to_string(img, config='-psm 0')
The error that comes up is
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 126, in image_to_string
f = open(output_file_name, 'rb')
IOError: [Errno 2] No such file or directory:
'/var/folders/m8/pkg0ppx11m19hwn71cft06jw0000gp/T/tess_uIaw2D.txt'
When I go into '/var/folders/m8/pkg0ppx11m19hwn71cft06jw0000gp/T', there's a file called tess_uIaw2D.osd that seems to contain the output information I was looking for. It seems like tesseract is saving a file as .osd, then looking for that file but with a .txt extension. When I run tesseract through the command line with --psm 0, it saves the output file as .osd instead of .txt.
Is it correct that pytesseract's image_to_string() works by saving an output file somewhere and then automatically reading that output file? And is there any way to either set tesseract to save the file as .txt, or to set it to look for a .osd file? I'm having no issues just running the image_to_string() function when I don't set the psm.
You have a couple of questions here:
PSM error
In your question you mention that you are running "--psm 0" in the command line. However in your code snip you have "-psm 0".
Using the double dash, config= "--psm 0", will fix that issue.
If you read the tesseract command line documentation, you can specify where to output the text read from the image. I suggest you start there.
Is it correct that pytesseract's image_to_string() works by saving an output file somewhere and then automatically reading that output file?
From my usage of tesseract, this is not how it works
pytesseract.image_to_string() by default returns the string found on the image. This is defined by the parameter output_type=Output.STRING, when you look at the function image_to_string.
The other return options include (1) Output.BYTES and (2) Output.DICT
I usually have something like text = pytesseract.image_to_string(img)
I then write that text to a log file
Here is an example:
import datetime
import io
import pytesseract
import cv2
img = cv2.imread("pathToImage")
text = pytesseract.image_to_string(img, config="--psm 0")
ocr_log = "C:/foo/bar/output.txt"
timestamp_fmt = "%Y-%m-%d_%H-%M-%S-%f"
# ...
# DO SOME OTHER STUFF BEFORE WRITING TO LOG FILE
# ...
with io.open(ocr_log, "a") as ocr_file:
timestamp = datetime.datetime.now().strftime(timestamp_fmt)
ocr_file.write(f"{timestamp}:\n====OCR-START===\n")
ocr_file.write(text)
ocr_file.write("\n====OCR-END====\n")
I am trying to display an image from a Url however i am not sure how to do so.
Below is my attempt:
imageFile = "http://photo.elsoar.com/wp-content/images/Personal-computer.jpg"
image1 =PhotoImage(open(imageFile))
image1.grid(row=1, column=5)
This just produces an error
Traceback (most recent call last):
File "C:\Users\Derren\Desktop\Online Shopper.py", line 131, in <module>
image1 =PhotoImage(open(imageFile))
IOError: [Errno 22] invalid mode ('r') or filename: 'http://photo.elsoar.com/wp-content/images/Personal-computer.jpg'
This is the image i want to have
http://photo.elsoar.com/wp-content/images/Personal-computer.jpg
However it is essential that it is resourced from the internet and not local files
The function open() only works on locals files. You can see doc here part 7.2. If you want to work with an online image, you can use this :
from PIL import Image
import requests
from io import BytesIO
response = requests.get("url")
img = Image.open(BytesIO(response.content))
img.save("my_img.png") #with the good filename extension
If you particularly want a PhotoImage based solution that this should work:
import urllib
f = urllib.urlopen("http://photo.elsoar.com/wp-content/images/Personal-computer.jpg")
data=f.read()
root = Tkinter.Tk()
image1 =PhotoImage(data)
I'm trying to convert .r8 files to ppm files using a python scrpit.
The scrpit works when converting .jpg files, but doesn't seem to work with .r8 files.
import PIL.Image
import os, os.path, string, sys
openfile = '/Users/.../.../Image_converter'
im = PIL.Image.open('Test0.r8')
im = im.convert('RGB')
im.save('Test0.ppm')
im.show('Test0.ppm')
Traceback (most recent call last):
File "R8_to_PPM.py", line 7, in <module>
im = PIL.Image.open('Test0.r8')
File "/Users/FirstDawn/anaconda/lib/python2.7/site-packages/PIL/Image.py", line 2286, in open
% (filename if filename else fp))
IOError: cannot identify image file 'Test0.r8'
Is there anyway to make this conversion using python? (Or another language?)
I'm using python 2.7 on a MacOS. (.r8 = raw graphics (one byte per pixel) plane one (PicLab))
Thank you.
I want to convert a jpg file to png, but when I run this code :
from opencv import _cv
from opencv.highgui import cvSaveImage, cvLoadImage
cvSaveImage("bet.jpg",cvLoadImage("bet.jpg"))
if __name__ == '__main__':
pass
It gives this error which I don't understand :
Traceback (most recent call last):
File "convert.py", line 6, in <module>
cvSaveImage("bet.jpg",cvLoadImage("bet.jpg"))
File "/usr/lib/pymodules/python2.6/opencv/highgui.py", line 183, in cvSaveImage
return _highgui.cvSaveImage(*args)
RuntimeError: openCV Error:
Status=Null pointer
function name=cvGetMat
error message=NULL array pointer is passed
file_name=cxarray.cpp
line=2780
I have my picture with the same folder of source code and the name of the image is bet.jpg
Any idea ??
The best choice is pyopencv:
import pyopencv as cv
img = cv.imread('01.png')
cv.imshow('img-windows',img)
cv.waitKey(0)
cv.imwrite('01.png',img)
From Python CV documentation, the CV2 method for converting a jpeg to png is:
Python: cv2.imwrite(filename, img[, params]) → retval
For my example:
import cv2
filename = 'pic.jpeg'
cam = cv2.VideoCapture(filename)
s, img = cam.read()
picName = 'pic.png'
cv2.imwrite(picName, img)
VideoCapture is nice and general, and works with videos, webcams and image files.
I solved the problem, the image I took randomly from the Google Images doesn't load. Maybe it's encrypted or something I don't know. I tried it with other images, and worked very well. So watch out while copying images : )