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
Jorre Goossens
  • 139
  • 1
  • 2
  • 10

3 Answers3

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.

Ankur Raj
  • 11
  • 1
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?

ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • 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? – Jorre Goossens Oct 11 '17 at 08: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 '17 at 09: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. – Jorre Goossens Oct 11 '17 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 https://stackoverflow.com/questions/8827016/matplotlib-savefig-in-jpeg-format – ralf htp Oct 11 '17 at 12:18
  • Yeah, for now I'm going to circumvent the problem by using only .jpg. Hopefully somewhere in the future it will become clear what the problem is. Thanks for the effort! – Jorre Goossens Oct 11 '17 at 12:49
  • 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(). – Greg Mansio Dec 07 '22 at 12:03