【bug】global loadsave.cpp:241 cv::findDecoder imread_(‘xxx‘):: can‘t open/read file

问题 OPENCV读取图片报错(win11)

[ WARN:[email protected]] global loadsave.cpp:241 cv::findDecoder imread_('xxx.png'): can't open/read file: check file path/integrity

解决:在windows系统上如果直接使用路径加载图片会报上述错误,使用PIL加载也无济于事,问题在文件路径的格式。因此博主做了如下更改。

        # from PIL import Image
        # pil_img = Image.open(self.current_image).convert('RGB')
        # self.current_image = np.array(pil_img)

        #absolute_path = os.path.abspath(self.current_image_path)
        current_image_path = 'xxx/xxxx/xxx.jpg'
        current_image_path = os.path.normpath(current_image_path)
        print(rf"{current_image_path}")
        from pathlib import Path
        # 创建Path对象
        path_obj = Path(current_image_path)

        # 强制转换为正斜杠表示
        converted_path = np.asarray(bytearray(path_obj.read_bytes()), dtype=np.uint8)
        print(converted_path)
        # 加载图像
        current_image = cv2.imdecode(converted_path, cv2.IMREAD_COLOR)
        if current_image is None:
            queue.put(("error", f"无法加载图像: {current_image_path}"))
            return

        # 转换为RGB
        current_image = cv2.cvtColor(current_image, cv2.COLOR_BGR2RGB)

你可能感兴趣的:(bug,pytorch,opencv)