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
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.