5

I am currently using Anaconda 4.3.27, Python 3.6.2 and OpenCV 3.3.0

When I try

img1 = cv2.imread('D:\Images\3D-Matplotlib.png')
img2 = cv2.imread('D:\Images\mainsvmimage.png')

I get libpng error: Read Errorand a pop-up shows up, indicating that Python stopped working. I already tried replacing the '\' by '\\' and '/', but also in those cases the same error shows up. When I try to read a jpg instead of a png, I do not get the error. Does anybody have an idea what might be the problem here?

Thanks in advance!

Edit:

Also cv2.imwrite gives an error:

libpng error: Write Error
2
  • do the images exist with given path? may be you can try os.listdir("D:/Images/")
    – Mitiku
    Oct 11, 2017 at 8:24
  • Yeah they are. When I try os.listdir, they are returned... Oct 11, 2017 at 8:31

3 Answers 3

2

Had the same issue with Anaconda using Matplotlib with Latex. The solution was to update libpng. https://github.com/ContinuumIO/anaconda-issues/issues/6271

1

I had broken images in my directory, removing those images solved the error.

0

try to add the flags (grayscale,...) that are required by cv2.imread ( https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html )

and use r for the path

 img2 = cv2.imread(r"D:\Images\mainsvmimage.png",0)

( the 0 loads image as grayscale )

OpenCV Python not opening images with imread()

if this is still not working maybe test with another image as it is possible that there is a problem with the image header cf. libpng error: Read Error or with your libpng version, cf. Libpng conflict on OpenCV?

6
  • Adding a flag and using 'r' in front of the path doesn't work. I tried saving the jpg that I could open as an png, but when I try reading it with imread, I get the same error. If the problem would be caused by the libpng version, shouldn't I be getting a different error like in the 'Libpng conflict on OpenCV?'-link? Oct 11, 2017 at 8:53
  • can you open a .jpg with img2 = cv2.imread(r"D:\Images\jpg_image.jpg.",0) ? normally this should work and if it works the problem is either related to libpng or with the headers of your pngs or permission problems, normally the python code above is correct
    – ralf htp
    Oct 11, 2017 at 9:08
  • Yes, I can open a .jpg. I now tried to update conda and libpng, but I still got the same error after it was updated. Is there a way to narrow down where my error is coming from? I'm still quite new to Python and OpenCV, so I don't have that much experience yet. Oct 11, 2017 at 12:07
  • i don't know, very likely this is libpng error and i am not familiar with windows. the libpng version has to match the version of opencv. if you are not fixed to .png use .jpg files and skip the libpng bug tracking. from matplotlib you can save files as .jpg like in stackoverflow.com/questions/8827016/…
    – ralf htp
    Oct 11, 2017 at 12:18
  • 1
    I had a similar issue when trying to read images with OpenCV imread() function. The solution was to "clean" the path. There were spaces and "+" in the filepath, and by replacing both with "_" the files are now properly read by cv2.imread(). Dec 7, 2022 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.