在使用 Python Matplotlib 库的 'imread' 函数读取图像文件时,可能会遇到以下两种常见错误:

  1. 'Invalid argument' 错误:

    Traceback (most recent call last):
      File '..py', line 14, in <module>
        img=mping.imread('D:\桌面\1.png')
      File 'C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\matplotlib\image.py', line 1563, in imread
        with img_open(fname) as image:
      File 'C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\PIL\ImageFile.py', line 105, in __init__
        self.fp = open(fp, 'rb')
    OSError: [Errno 22] Invalid argument: 'D:\桌面\1.png'
    

    该错误通常是由于文件路径中包含中文字符导致的。解决方法:将文件路径中的中文字符改为英文字符。

  2. 'not a PNG file' 错误:

    Traceback (most recent call last):
      File '..py', line 14, in <module>
        img=mping.imread('D:\桌面\1.png')
      File 'C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\matplotlib\image.py', line 1563, in imread
        with img_open(fname) as image:
      File 'C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\PIL\ImageFile.py', line 117, in __init__
        self._open()
      File 'C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\PIL\PngImagePlugin.py', line 705, in _open
        raise SyntaxError(msg)
    SyntaxError: not a PNG file
    

    该错误意味着您尝试读取的文件并非有效的 PNG 文件。可能的原因包括:

    • 文件损坏
    • 文件格式不正确

    解决方法:

    • 重新下载该文件
    • 使用其他有效的 PNG 文件进行测试

通过以上方法,您就可以解决 'imread' 函数读取图像文件时遇到的 'Invalid argument' 和 'not a PNG file' 错误,顺利读取图像文件。

Python Matplotlib 'imread' 错误: 'Invalid argument' 和 'not a PNG file' 解决方法

原文地址: https://www.cveoy.top/t/topic/phSK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录