Issue
When I try to run my code in PyCharm, it exits with code 0, and gives the desired output, but when I try to run it in VS Code it gives the following error:
File "c:\Users\1\Desktop\ImagetoText\ITT2.py", line 21, in <module> img = cv.cvtColor(img, cv.COLOR_BGR2RGB) cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-kh7iq4w7\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
How it is possible that the same code runs without errors or warnings relating to this line in PyCharm while not working in VS Code or directly in W10 is alien to my understanding.
Note: I have tried tweaking the path but to no avail.
Code:
from glob import glob
from io import BytesIO
import pytesseract
import cv2 as cv
from tkinter import *
import pyperclip
import os
presentItem = ImageGrab.grabclipboard()
with BytesIO() as f:
presentItem.save(f, format='PNG')
presentItem.save('tempITT' + '.png', 'PNG')
pytesseract.pytesseract.tesseract_cmd = 'C:\\Users\\1\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe'
img = cv.imread(r"C:\Users\1\Desktop\ImagetoText\tempITT.png")
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
imgtext = pytesseract.image_to_string(img)
pyperclip.copy(imgtext)
os.remove(r"C:\Users\1\Desktop\ImagetoText\tempITT.png")
Solution
Firts check if you really have this image.
C:\Users\1\Desktop\ImagetoText\tempITT.png
imread
doesn't show error when it can't find image but it returns None
and later code run cv.cvtColor(None, cv.COLOR_BGR2RGB)
which gives error with text !_src.empty()
I think all your problem starts with presentItem.save(...)
because you use filename without full path - so it may save it in local folder, not in C:\Users\1\Desktop\ImagetoText
, and later imread(r'C:\Users\1\Desktop\ImagetoText\...
) can't find it.
You should use full path in all functions
presentItem.save(r'C:\Users\1\Desktop\ImagetoText\tempITT.png', 'PNG')
BTW:
When you have code C:\Users\1\Desktop\ImagetoText
and you run it from this folder
cd C:\Users\1\Desktop\ImagetoText
python script.py
then presentItem.save("tempITT.png")
saves file in this folder C:\Users\1\Desktop\ImagetoText
and you have C:\Users\1\Desktop\ImagetoText\tempITT.png
,
but if you run code for other folder
cd other folder
python C:\Users\1\Desktop\ImagetoText\script.py
then presentItem.save("tempITT.png")
saves file in other foler you have C:\other folder\tempITT.png
And this can happend in your situation. Different tools may runs it in different way and later presentItem.save(
may save file in different folder - and you should use full path in presentItem.save()
Folder in which code is executed is called Current Working Directory
and you can see it using print( os.getcwd() )
Answered By - furas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.