Search code examples
pythonmatplotlibtiff

Cannot find TIF file


I'm trying to image a TIF file as a colormap but for some reason Python cannot seem to find the file I want it to image. I have installed matplotlib and Pillow.

Currently, these are the commands I'm giving it:

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

Although this doesn't image it as a colormap, I believe I saw on another post here that it could me modified into a colormap easily enough. Either way, this gives me the following error:

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'

Solution

  • Assign the path of the file to the filename and use it in the function imread() or directly provide the filename to the function like imread('file.tif')

    filename = 'file.tif'
    

    and then:

    imread(filename)