OpenCV TypeError: Expected on iteration files in directory Python
OpenCV TypeError: Expected on iteration files in directory Python
我在转换目录中的文件时遇到问题。它给了我一个 TypeError,我不确定如何修复它。
下面我将我的代码与输出一起粘贴。
directory = './Input/'
for filename in os.scandir(directory):
print(filename)
print(type(filename))
# image = cv2.imread(filename)
result = sr.upsample(filename)
cv2.imwrite("./Output/image.png", result)
我的输出:
<DirEntry '1.png'>
<class 'posix.DirEntry'>
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-14-e9096dfd223e> in <module>()
28
29 # image = cv2.imread(filename)
---> 30 result = sr.upsample(filename)
31 # cv2.imwrite("./Output/image.png", result)
32 # else:
TypeError: Expected Ptr<cv::UMat> for argument 'img'
upsample 函数获取图像 Mat,而不是 DirEntry 或路径。您需要先阅读图片。
或许试试:
directory = './Input/'
for filename in os.scandir(directory):
if (filename.path.endswith(".jpg") or filename.path.endswith(".png")):
print(filename.path)
image = cv2.imread(filename.path)
result = sr.upsample(image)
cv2.imwrite("./Output/image.png", result)
我在转换目录中的文件时遇到问题。它给了我一个 TypeError,我不确定如何修复它。 下面我将我的代码与输出一起粘贴。
directory = './Input/'
for filename in os.scandir(directory):
print(filename)
print(type(filename))
# image = cv2.imread(filename)
result = sr.upsample(filename)
cv2.imwrite("./Output/image.png", result)
我的输出:
<DirEntry '1.png'>
<class 'posix.DirEntry'>
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-14-e9096dfd223e> in <module>()
28
29 # image = cv2.imread(filename)
---> 30 result = sr.upsample(filename)
31 # cv2.imwrite("./Output/image.png", result)
32 # else:
TypeError: Expected Ptr<cv::UMat> for argument 'img'
upsample 函数获取图像 Mat,而不是 DirEntry 或路径。您需要先阅读图片。
或许试试:
directory = './Input/'
for filename in os.scandir(directory):
if (filename.path.endswith(".jpg") or filename.path.endswith(".png")):
print(filename.path)
image = cv2.imread(filename.path)
result = sr.upsample(image)
cv2.imwrite("./Output/image.png", result)