我试图遍历目录并使用scikit-image调整每个图像的大小,但我不断收到以下错误:
b'scene01601.png'
Traceback (most recent call last):
File "preprocessingdatacopy.py", line 16, in <module>
image_resized = resize(filename, (128, 128))
File "/home/briannagopaul/PycharmProjects/DogoAutoencoder/venv/lib/python3.6/site-packages/skimage/transform/_warps.py", line 104, in resize
input_shape = image.shape
AttributeError: 'str' object has no attribute 'shape'
我的代码:
import skimage
from sklearn import preprocessing
from skimage import data, color
import os
from skimage.transform import resize, rescale
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import os
directory_in_str = "/home/briannagopaul/imagemickey/"
directory = os.fsencode(directory_in_str)
for file in os.listdir(directory):
print(file)
filename = os.fsdecode(file)
if filename.endswith(".png"):
image_resized = resize(filename, (128, 128))
img = mpimg.imread(file)
imgplot = plt.imshow(img)
plt.show()
filename.shape()
答案 0 :(得分:1)
首先,除非代码在与映像相同的目录中运行,否则您将要在文件名中指定目录:
for file in os.listdir(directory):
print(file)
filename = directory_in_str + os.fsdecode(file)
但是要解决您的问题,您已经在通过mpimg.imread
行读取图像并将该图像存储为名为img
的numpy数组。使用该img
变量,您可以在其余各行中运行它:
if filename.endswith(".png"):
img = mpimg.imread(filename)
image_resized = resize(img, (128, 128))
imgplot = plt.imshow(img)
plt.show()
print(img.shape)
请注意,我将两个单独的调用从filename
更改为img
。这是因为filename
只是文件名,而不是实际文件,在您的情况下称为img
。