Search code examples
python-3.xopencvimage-processinglibpngsrgb

OpenCV strip sRGB profile when imdecode image Python


I am reading in images which I fetch from the internet and then immediately read into OpenCV in python as below:

# read in image as bytes from page
image = page.raw_stream.read()
frame = cv2.imdecode(np.asarray(bytearray(image)), 0)

and I am getting the libpng warning:

libpng warning: iCCP: known incorrect sRGB profile

How can I strip the sRGB profile before imread? People are suggesting to do it via imagemagick on the png files before reading them but this is not possible for me. Is there no way to do this in python directly?

EDIT:

I am unable to get the code in the answer below to fix my problem, if I run it with the file at https://uploadfiles.io/m1w2l and use the code:

import cv2
import numpy as np

with open('data/47.png', 'rb') as test:
   image = np.asarray(bytearray(test.read()), dtype="uint8")
   image = cv2.imdecode(image, cv2.IMREAD_COLOR)

I get the same warning


Solution

  • Using urllib:

    import cv2
    import urllib
    
    resp = urllib.request.urlopen('https://i.imgur.com/QMPkIkZ.png')
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    

    Using skimage:

    import cv2
    from skimage import io
    
    image = io.imread('https://i.imgur.com/QMPkIkZ.png')
    image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGRA)
    

    If you get a weird display using OpenCV cv2.imshow, keep in mind that OpenCV does not display alpha channels.