1

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

kabeersvohra
  • 1,049
  • 1
  • 14
  • 31
  • Can you download one of these files on disk and then read with `cv2.imread`? Also post a link to one of these files. – zindarod May 25 '18 at 11:45
  • I was doing that before but it significantly slowed down the code having to save and read from a file with the volume of images I was processing, here is one of the files: https://imgur.com/a/3w21Mrh – kabeersvohra May 25 '18 at 11:54

1 Answers1

5

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.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
zindarod
  • 6,328
  • 3
  • 30
  • 58
  • Thank you for your help, I think however that imgur actually fixes the sRGB on their server. Could you please use this file to test and the code that I have updated the question with: https://uploadfiles.io/m1w2l thanks! – kabeersvohra May 25 '18 at 16:00