imread throws [WARN] can't open/read file: check file path/integrity - python

what can i do?
WARN:0#0.726] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('pythonProject6/arrow.jpg'): can't open/read file: check file path/integrity
Traceback (most recent call last):
File "C:\Users\PC2\PycharmProjects\pythonProject6\whatsapp try.py", line 17, in <module>
cv2.imshow("image", img)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-
python\opencv\modules\highgui\src\window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
import cv2
img = cv2.imread("pythonProject6/arrow.jpg")
#print(img.shape)
cv2.imshow("image", img)
cv2.waitkey(0)

first of all, make sure you have defined default folder in python correctly. then check the file and address spelling. if it did not resolve, then try this:(use r and instead of / use //.
import cv2
img = cv2.imread(r"pythonProject6//arrow.jpg")
#print(img.shape)
cv2.imshow("image", img)
cv2.waitkey(0)
If it did not resolve, check your file attribution, and uncheck hidden and read-only. always check you can view the image by windows photo viewer.

I had the same problem using VS Code on macOS. The path was correct, but I was getting the same warning, and the resulting image of cv2.imread was empty.
The issue was solved when I gave Full Disk Access to VS Code. I figured it out because I managed to run the code using my terminal (instead of VS Code terminal).
System Settings -> Privacy & Security -> Full Disk Access

The latest versions of opencv 4.6.x.y and later have the cv2.imshow() issue. I experienced the same. If solution is:
First
pip3 uninstall opencv-python
Second:
pip3 install opencv-python==4.5.5.62

Related

PyCharm can't find Tesseract on Mac

I'm starting out with some basic python tutorials with OpenCV, and the first tutorial uses Tesseract, Pytesseract, and OpenCV. I have Tesseract downloaded and pip installed, and I have Pytesseract and OpenCV downloaded, installed, and included in my PyCharm packages, so I think the problem is how I'm addressing the Tesseract file in my code, since I'm new to using a Mac.
(I'm using Python 3.8, but also have Python 2.7 installed, because I needed it to get to this point. Weirdly enough, up to this point, the code only ran without error if I had Python 2.7 installed, but had 3.8 as my PyCharm interpreter.)
When I put Tesseract into my terminal, it tells me that the file address is simply 'Applications/tesseract'. But when I use this as the address in PyCharm, I get the error message below. If anyone could help me figure out how to handle this error, I would appreciate it a lot!!! (I'm new to everything computers, btw. This is how I'm learning.)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/george/PycharmProjects/pythonProject2/main.py", line 7, in <module>
print(pytesseract.image_to_string(img))
File "/Users/george/Library/Python/3.8/lib/python/site-packages/pytesseract/pytesseract.py", line 370, in image_to_string
return {
File "/Users/george/Library/Python/3.8/lib/python/site-packages/pytesseract/pytesseract.py", line 373, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "/Users/george/Library/Python/3.8/lib/python/site-packages/pytesseract/pytesseract.py", line 282, in run_and_get_output
run_tesseract(**kwargs)
File "/Users/george/Library/Python/3.8/lib/python/site-packages/pytesseract/pytesseract.py", line 254, in run_tesseract
raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: \Applications\tesseract is not installed or it's not in your PATH. See README file for more information."
I don't know what to look for in the README file, though.
Here is the code that kicked off the error message:
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = '\\Applications\\tesseract'
img = cv2.imread('im1.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print(pytesseract.image_to_string(img))
cv2.imshow('Result', img)
cv2.waitKey(0)
On Macs (and other Unix-like OSes), the path separator is the forward slash, not the backslash.
pytesseract.pytesseract.tesseract_cmd = '/Applications/tesseract'
could work better.
However, do you actually need to explicitly set that? It's likely the library could be able to find Tesseract on its own

How to read image from file directory in Jupyter Notebook using OpenCV?

I am trying to read an image from the laptop in Jupyter notebook using imread() of OpenCV. When I execute the program by specifying the address of the file, it shows the following error:
img2 = cv.imread('D:\Photos\roaches.jpg')
cv.imshow("Cockroaches", img2)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
error Traceback (most recent call last)
<ipython-input-8-645759bb8eb6> in <module>
1 img2 = cv.imread('D:\Photos\roaches.jpg')
----> 2 cv.imshow("Cockroaches", img2)
3 cv.waitKey(0)
4 cv.destroyAllWindows()
error: C:\ci\opencv_1512688052760\work\modules\highgui\src\window.cpp:331: error: (-215) size.width>0 && size.height>0 in function cv::imshow
Screenshot of the notebook snippet
However, when I placed the image in the directory where Jupyter notebook is running, the code worked fine.
How can I read any image from the computer without placing it in the Jupyter notebook directory?

Capturing image from Webcam Using Python on Windows

the code i am using now is :-
from VideoCapture import Device
cam = Device()
cam.saveSnapshot('image.jpg')
using py 2.7
and imported pygame and all and videocapture
i am getting this error in pycharm :-
C:\Python27\python.exe F:/Xtraz/Orion/Key-Logger.py
Traceback (most recent call last):
File "F:/Xtraz/Orion/Key-Logger.py", line 3, in <module>
cam.saveSnapshot('image.jpg')
File "C:\Python27\lib\VideoCapture.py", line 200, in saveSnapshot
self.getImage(timestamp, boldfont, textpos).save(filename, **keywords)
File "C:\Python27\lib\VideoCapture.py", line 138, in getImage
im = Image.fromstring('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)
File "C:\Users\admin\AppData\Roaming\Python\Python27\site-packages\PIL\Image.py", line 2080, in fromstring
"Please call frombytes() instead.")
NotImplementedError: fromstring() has been removed. Please call frombytes() instead.
Process finished with exit code 1
the webcam LED lights onn , and then switches off immediately .
or help me with any other code and library that works well with py 2.7 and pycharm on windows only ! and i just want to save the image , not display it !
You might want to downgrade you version of PIL, it seems like VideoCapture hasn't been updated for a while and is still relying on outdated versions of PIL.
PIL 2.x seems to have a working fromstring method: https://github.com/python-pillow/Pillow/blob/2.9.0/PIL/Image.py#L750
Otherwise you can try to change the line 138 in VideoCapture.py from im = Image.fromstring(...) to im = Image.frombytes(...); hopefully it's the only thing that prevents it from working.
Solution #1: Downgrade PIL
If you're using pip, you can just uninstall you current version using pip uninstall Pillow and then install an older one using pip install Pillow==2.9.0 (Pillow is a fork of PIL, PIL being basically dead).
Solution #2: Update VideoCatpure
Open the file C:\Python27\lib\VideoCapture.py and go to line 138. You should have something like that:
im = Image.fromstring('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)
Replace this line with this:
im = Image.frombytes('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)

python use opencv get an error

When I use the opencv with the python, I always get an error.
I set the environment like this:
install the python "python-2.7.5.msi"
install the numpy "numpy-MKL-1.7.1.win32-py2.7.exe"
install the opencv "opencv-python-2.4.6.win32-py2.7.exe"
Everything is OK. I test it using the following code:
import cv2
img = cv2.imread('lena.bmp')
cv2.show('Image', img)
cv2.waitKey(0)
But I got an error as follow:
File "E:\Python\cv2.py", line 1, in <module>
import cv2
File "E:\Python\cv2.py", line 2, in <module>
img = cv2.imread('lena.bmp')
AttributeError: 'module' object has no attribute 'imread'
Why? But when I print "import cv2" in the IDLE, I didn't get any error.
well, I guess you imported your file, ie the file you are writing. cause the file is using the name "cv2.py" as it suggested. you`d better change the filename.

Python: PIL - [Errno 32] Broken pipe when saving .png

What I'm trying to do here is save the contents of a Tkinter Canvas as a .png image using PIL.
This is my save function ('graph' is the canvas).
def SaveAs():
filename = tkFileDialog.asksaveasfilename(initialfile="Untitled Graph", parent=master)
graph.postscript(file=filename+".eps")
img = Image.open(filename+".eps")
img.save(filename+".png", "png")
But it's getting this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Users\Adam\Desktop\Graphing Calculator\Graphing Calculator.py", line 352, in SaveAs
img.save(filename+".png", "png")
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1406, in save
self.load()
File "C:\Python27\lib\site-packages\PIL\EpsImagePlugin.py", line 283, in load
self.im = Ghostscript(self.tile, self.size, self.fp)
File "C:\Python27\lib\site-packages\PIL\EpsImagePlugin.py", line 72, in Ghostscript
gs.write(s)
IOError: [Errno 32] Broken pipe
I'm running this on Windows 7, Python 2.7.1.
How do I make this work?
oh I just get the same error. I have solve it now
just do the following after installing PIL and Ghostscript
1) Open C:\Python27\Lib\site-packages\PIL\EpsImagePlugin.py
2) Change code near line 50 so that it looks like this:
Build ghostscript command
command = ["gswin32c",
"-q", # quite mode
"-g%dx%d" % size, # set output geometry (pixels)
"-dNOPAUSE -dSAFER", # don't pause between pages, safe mode
"-sDEVICE=ppmraw", # ppm driver
"-sOutputFile=%s" % file,# output file
"-"
]
Make sure that gswin32c.exe is in the PATH
good luck
It looks like the Ghostscript executable is erroring out and then closing the connection. Others have had this same problem on different OSes.
So, first I would recommend that you confirm that PIL is installed correctly--see the FAQ page for hints. Next, ensure that Ghostscript is installed and working. Lastly, ensure that Python can find Ghostscript, for example by running a PIL script that works elsewhere.
Oh, also--here are some tips on catching the broken pipe error so your program can be more resilient, recognize the problem, and warn the end-user. Hope that helps!
I have realized that while Python 2.7 has this EPEImagePulgin.py, Anaconda also has it. And unfortunately Anaconda's file is an older version. And unfortunately again, when you run your from Spyder environment it was picking up the epsimageplugin.py file from anaconda folder.
So I was getting similar broken pipe error.
When I went into python 2.7 directory and opened python console and then ran my code, it ran just fine.
Because the lates epsimageplugin.py file takes into consideration the windows environment and appropriate ghostscript exe files. Hope this helps.

Categories