找不到TIF文件

问题描述 投票:0回答:2

我正在尝试将TIF文件成像为颜色图,但由于某些原因,Python似乎找不到我想要对其成像的文件。我已经安装了matplotlib和Pillow。

当前,这些是我提供的命令:

import matplotlib.pyplot as plt
import matplotlib.image as mping
img=mping.imread('filename')

尽管这不会将其成像为颜色图,但我相信我在这里的另一篇文章中看到,我可以很容易地将其修改为颜色图。无论哪种方式,这都会给我以下错误:

Traceback (most recent call last):

  File "<stdin>", line 1, in < module >

  File "C:\Users\query\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\image.py", line 1417, in imread
    with Image.open(fname) as image:

  File "C:\Users\query\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2809, in open
    fp = builtins.open(filename, "rb")

FileNotFoundError: [Errno 2] No such file or directory: 'filename'
python matplotlib tiff
2个回答
0
投票

为了让python找到要映像的文件,需要给它提供文件的文件路径,我将假定您的文件未命名为filename,它实际上具有一些扩展名,因此您的功能会更多像-

import matplotlib.pyplot as plt
import matplotlib.image as mping
img=mping.imread("source_data\\tif_files\\tif_data.tif")

[用文件路径替换"source_data\\tif_files\\tif_data.tif"的位置,相对于您从中或显式调用脚本的位置。


-1
投票

将文件的路径分配给文件名,然后在函数imread()中使用它,或者直接将文件名提供给函数,例如imread('file.tif')

filename = 'file.tif'

然后:

imread(filename)
© www.soinside.com 2019 - 2024. All rights reserved.