Click here to Skip to main content
15,799,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Help Pls
My code:
<pre>img_file = 'test2.png'

img = cv2.imread(img_file, 1)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
print(f"Loaded {img_file} ({img.shape[1]} x {img.shape[0]})" )

# Resize if image larger than 2k pixels on a side
if img.shape[1] > 2000 or img.shape[0] > 2000:
    print(f"Image too big ({img.shape[1]} x {img.shape[0]})")
    new_size = 800.0 # px
    if img.shape[1] > img.shape[0]:
        # resize by width to new limit
        ratio = new_size / img.shape[1]
        # resize by height
        ratio = new_size / img.shape[0]
    print(f"Reducing by factor of {(1./ratio)}" )
#     img = cv2.resize(img,(0,0),fx=ratio,fy=ratio)
    print (f"New size: ({img.shape[1]} x {img.shape[0]})")

# See original image

Error Message:
error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-sn_xpupm\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

error                                     Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_7268/ in <module>
      3 img = cv2.imread(img_file, 1)
----> 4 img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
      5 print(f"Loaded {img_file} ({img.shape[1]} x {img.shape[0]})" )

What I have tried:

img = cv2.imread(img_file, 1)

i changed it with below code it didn't work
img = cv2.imread(img_file)
Updated 22-Nov-21 19:14pm
Richard MacCutchan 30-Jul-21 3:33am    
The message suggests that your image file is not being read in.
CL4Y3R-TR 30-Jul-21 9:20am    
it creates the image file itself
Richard MacCutchan 30-Jul-21 10:42am    
What does that mean? I ran exactly the same code, with an incorrect filename, and received exactly the same error. So you need to do some debugging to see exactly why your sample is failing.

1 solution

That is because it cannot find the img_file
img = cv2.imread(img_file)

so, write in the full path for the img_file name,
maybe the problem will be solve.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900