1

while using this code to open a png in memory , libpng give me this warning/error:

cv2.imread("477px-PNG_Test-Wrong-Width.0_Fixed.png")
libpng error: bad adaptive filter value

This is exactly what i want because i need to retrieve informations about corrupted png in order to fix them.

But iv found myself unable to put this warning/error inside a var because as iv understood libpng use stderr to print those warnings .

So i tried many solutions to redirect stderr/stdout to a variable like this one for python >= 3.5:

import io , cv2
from contextlib import redirect_stderr

with io.StringIO() as buf, redirect_stderr(buf):
    cv2.imread("477px-PNG_Test-Wrong-Width.0_Fixed.png")
    output = buf.getvalue()

But it does not work..It seems that this message is print from another level of stream than python .

Can anyone please help me ?

On4r4p
  • 33
  • 7
  • Why would you even consider using OpenCV for this? It's a computer vision library. All it does is provide a convenience high-level wrapper for reading and writing valid images in several formats. – Dan Mašek Nov 07 '21 at 00:03
  • What should i use then ? – On4r4p Nov 07 '21 at 00:05
  • 1
    No idea of a concrete solution (and software recommendations are off topic here), but you'll have to go much lower level that what OpenCV provides. Either some direct wrapper around libpng, or maybe some pure Python implementation of a PNG codec. – Dan Mašek Nov 07 '21 at 00:08
  • I was afraid of this ..Anyway thnk you im going to dig that way . – On4r4p Nov 07 '21 at 00:12
  • 1
    One more thought -- if you want to just capture the warning it prints and that's enough, there might be a way with OpenCV. Have a look at this: https://stackoverflow.com/questions/49602465/how-to-stop-opencv-error-message-from-printing-in-python -- but it's been a while since I wrote that, things have changed, and it may not work in your case. – Dan Mašek Nov 07 '21 at 00:13

0 Answers0