Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib: ValueError: invalid PNG header

Tags:

matplotlib

import matplotlib.pyplot as plt

I tried to imread a png photo among many other png photos in the same folder. Some photos read with no errors using the following line, and some returns "ValueError: invalid PNG header". What could be the cause? They all look like normal photos to me.

plt.imread(filename)

like image 751
MichM Avatar asked Sep 07 '16 21:09

MichM


1 Answers

As @MichM has already stated that somehow the header of your image has changed and it is not a valid png image. Read the image as

plt.imread(filename, 0)

If you read the Matplotlib imread's doc at https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imread.html?highlight=imread#matplotlib.pyplot.imread

--> If format is provided, will try to read file of that type, otherwise the format is deduced from the filename. If nothing can be deduced, PNG is tried. <--

like image 124
Shivam Kotwalia Avatar answered Sep 19 '22 11:09

Shivam Kotwalia