I'm running into an interesting issue with opencv in python. I noticed that the luminance in the red channel is significantly reduced when I load an image using opencv and immediately save it without doing any further processing.
- I know that jpeg is a lossy format, and that I should expect quality to degrade if I read/write a bunch of times. However, if I open the image in preview (mac) and export from there, I don't see a difference, even if I export to the lowest possible quality. If I use openCV to do the exact same thing, the difference in red channel is enormous (much more than you would expect due to the compression).
- I tried to change all the flags (imwrite_jpeg_quality, imwrite_jpeg_luma_quality, imread_anycolor, imread_unchanged, etc). I could not find any combination that prevents this effect.
- To test whether it was just the red channel, I tried this with a picture of a green apple (no difference), and a picture of an orange (red reduction noticable by eye). To make sure it has nothing to do with the image file itself (corrupted?), I also took a screenshot of the orange, exported it as a new jpeg file, and tried to read/write with python, and again the color was changed.
- I attached the images before/after of the orange. The difference becomes very clear if you open both images in their own tab and switch back and forth.
- If I read an already processed image (with reduced red) using opencv, and export it a second time, the red is not reduced any further. It only happens when the image is processed by opencv the first time.
this is my code:
import cv2
loc = '/imagetest/'
img = cv2.imread(loc + 'orange.jpg', cv2.IMREAD_UNCHANGED)
cv2.imwrite(loc + 'orange_processed.jpg',img,[cv2.IMWRITE_JPEG_QUALITY, 100])