'OpenCV not able to read/write a jpg image [closed]
I got this very simple code to iterate through a directory, apply canny edge detection on every single image and save every result to a different directory:
import cv2, os
directory = os.listdir('beard/')
for image in directory:
cv2.imwrite('canny-esult/' + image + '_canny.jpg', cv2.Canny(cv2.imread('bear/' + image), 100, 200))
But this is the error I'm getting:
cv2.imwrite('canny-esult/' + image + '_canny.jpg', cv2.Canny(cv2.imread('bear/' + image), 100, 200))
cv2.error: OpenCV(4.5.5) /io/opencv/modules/imgcodecs/src/loadsave.cpp:801: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'
Solution 1:[1]
I found some errata
bear -> beard
cv2.imwrite('canny-esult/' + image + '_canny.jpg', cv2.Canny(cv2.imread('beard/' + image), 100, 200))
Solution 2:[2]
Assertion 215 states that the image size is 0. You've simply made a type in defining the image path which is why you are not able to read the image, and then you try to resize the image and it gives the error since there is no image. Just recheck your image path and once its correct, it should work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user2804719 |
| Solution 2 | Muhammad Ahmed |
