-1

I am working on a python script to resize all of my images inside of a directory to a set width and height. my issue is that it doesn't properly read my image when using cv2.resize(). I know there are solutions online that I can simply copy and paste and be on my way but I don't learn that way. Any help is greatly appreciated because I am here to not only be laughed at but to also learn.

I have looped through the directory making sure its actually seeing the filename and extension. instead of using height = 28 and width = 28 ive tried to use height= np.size(img, 0) and width = np.size(img, 1)

# This is my code. I loop through my directory of images using i to represent the file and attempt to resize it to 28x28
import cv2
import os
import numpy as np

width = 28
height = 28

for i in os.listdir("C:/Users/ryan/PycharmProjects/ML_Projects_fixed/Red"):
    img = cv2.imread(i)
    img = cv2.resize(img, (width, height))
    cv2.imwrite(i, img)

I am given this error which to me means I don't see an image:

Traceback (most recent call last): File "C:/Users/ryan/PycharmProjects/ML_Projects_fixed/resize_test.py", line 10, in cv2.resize(img, (width, height)) cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:3718: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

But when I do

for i in os.listdir("C:/Users/ryan/PycharmProjects/ML_Projects_fixed/Red"):
     print(i)

I get all my filenames + there extension example: 096.png 097.png 098.png 099.png

So why is it telling me that it doesn't see the images?

edit 1;

for i in os.listdir("C:/Users/ryan/PycharmProjects/ML_Projects_fixed/Red"):
     img = cv2.imread(i)
     print(img)

this loop prints None for all of the images. does this mean its a path issue?

edit 2; Thank you to Geza Kerecsenyi's answer it solved my question. my loop wasn't finding the designated file, it was only reading the filename so unless it was in the same path where I ran my script there is no way it would see it.

Thank you to all comments

  • Are there any files in that directory which aren’t images? Which file is it reading when you get the error? – Pam Jun 15 '19 at 14:11
  • Hi Pam, the directory is full of images that are all pngs and nothing else. I am going to post an update that I saw which is the output of cv2.imread(i) in a moment, thanks for the response – rustyranger Jun 15 '19 at 14:13
  • You might find my answer to [this](https://stackoverflow.com/questions/49070242/converting-images-to-csv-file-in-python/49070833#49070833) interesting because I create the list of files first (and then do the requested conversation). It’s more python but maybe clearer. – Pam Jun 15 '19 at 14:13
  • 1
    @rustyranger Are all of the file paths correct in the image? Try: `img = cv2.imread("C:/Users/ryan/PycharmProjects/ML_Projects_fixed/Red/" + i)`. That should fix it if it is a path issue. – Geza Kerecsenyi Jun 15 '19 at 14:16
  • Using the line that Geza suggested fixed it. It wasn't actually seeing where the images were located when I was doing img = cv2.imread(i). which is super obvious to me now because i is just the filename but i thought if i looped through the directory i wouldnt have to add the extension infront of I. thank you for the answer and pam thank you I will definetly be reading that it seems very cool. – rustyranger Jun 15 '19 at 14:18
  • @rustyranger if it helps, I've posted it as an actual answer now. – Geza Kerecsenyi Jun 15 '19 at 15:53
  • I am new to this website I apologize i couldn't figure out how to highlight comment – rustyranger Jun 15 '19 at 16:31

1 Answers1

0

Try: img = cv2.imread("C:/Users/ryan/PycharmProjects/ML_Projects_fixed/Red/" + i). It's most likely a path issue, because the cv2 module is located in a different place (C:\Users\ryan\AppData\Local\Programs\Python\Python36) than the JetBrains folder which is where it's being called from. You could probably fix this in Pycharm settings, though.

Geza Kerecsenyi
  • 1,127
  • 10
  • 27