TypeError: Image data cannot be converted to float [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. TypeError: Image data cannot be converted to float [Solved]
  2. Solving the error when using the opencv-python module

# TypeError: Image data cannot be converted to float [Solved]

The matplotlib "TypeError: Image data cannot be converted to float" occurs when you pass an incorrect argument to the plt.imshow() method.

To solve the error, load the image and pass it as an argument to plt.imshow().

Here is an example of how the error occurs.

main.py
import matplotlib.pyplot as plt # ⛔️ TypeError: Image data of dtype <U9 cannot be converted to float plt.imshow('house.webp') plt.show()

typeerror image data cannot be converted to float

We called the matplotlib.pyplot.imshow() method with an incorrect argument which caused the error.

The most common cause of the error is calling the method with a path to the image rather than the image itself.

To solve the error, load the image and pass it as an argument to pyplot.imshow() instead.

main.py
import matplotlib.pyplot as plt img = plt.imread('house.webp') plt.imshow(img) plt.show()

The code sample assumes that you have the following house.webp image in the same directory as your main.py script.

house

You can right-click on the image and select "Save image as" to download it.

Here is the result of running the python main.py command.

load image before showing it

We used the matplotlib.pyplot.imread() method to read the image from a file into an array.

The only argument we passed to the method is the filename.

The argument the pyplot.imread() method takes can be a filename or a file-like object opened in read-binary mode.

main.py
import matplotlib.pyplot as plt img = plt.imread('house.webp') plt.imshow(img) plt.show()

If you need to read an image from a URL, use the Pillow library instead.

main.py
import urllib.request import matplotlib.pyplot as plt import numpy as np from PIL import Image url = 'https://www.google.com/favicon.ico' img = np.array(Image.open(urllib.request.urlopen(url))) plt.imshow(img) plt.show()

solve error when reading from url

Make sure you have numpy and pillow installed by running the following command in your terminal.

shell
pip install numpy Pillow matplotlib # or with pip3 pip3 install numpy Pillow matplotlib

The code sample:

  1. Uses the urllib.request.urlopen() method to load the image from the remote URL.
  2. It then uses the PIL.Image() class to open and identify the image file.
  3. The last step is to use the numpy.array() method to convert the PIL image to an array.

We can then pass the image to the pyplot.imshow() method to display it.

The pyplot.imshow method can be called with an array-like object or a PIL image.

The supported array shapes are:

  1. (M, N) - an image with scalar data. The values are mapped to colors using normalization and a colormap.
  2. (M, N, 3) - an image with RGB values (0-1 float or 0-255 int).
  3. (M, N, 4) - an image with RGBA values (0-1 float or 0-255 int), e.g. including transparency.

The first two dimensions (M, N) define the rows and columns of the image.

Make sure you aren't trying to plot an image path.

Instead, load the image and plot the image itself.

You can also use the Pillow module to load the image from a local file.

main.py
import matplotlib.pyplot as plt import numpy as np from PIL import Image img = np.array(Image.open('house.webp')) plt.imshow(img) plt.show()

load image from local file before calling show

The Image.open() method opens and identifies the given image file.

The only parameter we passed to the method is a filename string.

The method can also be called with a pathlib.Path object or a file object.

# Solving the error when using the opencv-python module

You can also use the opencv-python module to load the image before showing it.

First, make sure you have the module installed.

Open your terminal and run the following command.

shell
pip install opencv-python # or with pip3 pip3 install opencv-python

Now import and use the module as follows.

main.py
import cv2 from matplotlib import pyplot as plt img = cv2.imread('house.webp') plt.imshow(img) plt.show()

using opencv module to solve the error

The cv2.imread method takes the path to the image as a parameter.

The method loads the image from the specified file and returns it.

If you run into issues when specifying the path of the file:

  1. Locate the image you want to load in File Explorer.
  2. Right-click on the image and copy the absolute path to the file.
  3. Pass the path to the cv2.imread() method.

If you are on Windows, use the r character to mark the path as a raw string.

main.py
file_name = r'C:\Users\YourUser\Desktop\your-image.png'

When we prefix the string with r, we treat backslashes as literal characters instead of escape characters.

You can also use a second backslash to escape the first.

main.py
file_name = 'C:\\Users\\YourUser\\Desktop\\your-image.png'

It is also valid to use forward slashes instead of backslashes.

main.py
file_name = 'C:/Users/YourUser/Desktop/your-image.png'

However, make sure you aren't using a single backslash in your path as single backslash characters are used as an escape sequence.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.