error: OpenCV(4.1.0) error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

Solution 1:

Generally this problem occurs due to resizing of the image, I just apply try and catch statement for resizing the image so that any error handling. It is working fine and not getting error related to shape.

 img=cv2.imread(filename)
    print(img)
        try:
           img = cv2.resize(img, (1400, 1000), interpolation=cv2.INTER_AREA)
            print(img.shape)
        except:
        break
    height, width , layers = img.shape
    size=(width,height)
    print(size)

Solution 2:

I had the same issue when I was importing images into a list through label containing the name and path of images.

My problem arose due to an empty space in the list of labels.

I fixed this problem by just removing the space from my label list:

enter image description here

Solution 3:

Cat[i] in your code is doing what,

if there are 1000 images in folder, you have to list the image directory (os.listdir) and read them 1 by 1 through loop applying your function.

other than that, your function for compression is correct and working properly, other thing I will suggest to convert in grayscale first and then apply resize, may result a better interpolation.