def convert_to_grayscale(self):
subdirectories = self.list_subdirectories()
for subdirectory_name in subdirectories:
subdirectory_path = os.path.join(self.base_dir, subdirectory_name)
image_files = [os.path.join(subdirectory_path, file) for file in os.listdir(subdirectory_path) if os.path.isfile(os.path.join(subdirectory_path, file))]
if image_files:
for image_path in image_files:
img = image.load_img(image_path)
img_array = image.img_to_array(img)
img_gray = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY)
img_gray = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2BGR) # Ensure it's 3-channel
cv2.imwrite(image_path, img_gray)
print("Grayscale conversion and saving completed.")
字符串
当调用此:image_processing.convert_to_grayscale()
时
我得到这个错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-50-feb64db6939a> in <cell line: 1>()
----> 1 image_processing.convert_to_grayscale()
<ipython-input-27-7a06c7c14fb3> in convert_to_grayscale(self)
52 for image_path in image_files:
53 img = image.load_img(image_path)
---> 54 if len(image.shape) == 3 and image.shape[2] == 3:
55 img = cv2.cvtColor(image.img_to_array(img), cv2.COLOR_BGR2GRAY)
56
AttributeError: module 'keras.utils' has no attribute 'shape'
型
解决这个问题,我尝试了不同的方法,我也使用chatgpt,但没有得到正确的答案。
1条答案
按热度按时间erhoui1w1#
将
image.shape
替换为img.shape
:字符串
替换:
型
有:
型
但是,如果
img
是PIL映像,则可以检查mode
属性:型