Conversion from OpenCV to PIL is not accurate

opencv resize
pil image
pil convert to grayscale
convert pil to cv
convert cv image to pil
bgr2rgb
convert image to pil image
convert opencv image to jpg

When I convert an image from PIL to OpenCV, colors change slightly.

from PIL import Image
import cv2
import numpy as np
image = cv2.imread('1.jpg')
image1=Image.open('1.jpg')
image1 = cv2.cvtColor(np.array(image1), cv2.COLOR_RGB2BGR)
print(image[79])
print(image1[79])

The first four rows are:

[144 151 148]
[101 108 105]
[121 128 125]
[108 118 112]

and

[140 152 146]
[ 97 109 103]
[117 129 123]
[104 118 112]

I thought the indexing may be off by one but it isn't. Is there a way to fix this? This is the image (but it's the same on others too):

That suggests PIL and OpenCV are using different versions of libjpeg or using it with different parameters. Also it seems OpenCV tries to use libjpeg-turbo if you don't ask it not to do so, see the code here: https://github.com/opencv/opencv/blob/master/cmake/OpenCVFindLibsGrfmt.cmake#L39

Convert opencv image format to PIL image format?, Yes OpenCV is more robust and flexible and can perform most of the image processing routines which are available out there, So probably this� Converting PIL image to OpenCV image. Hello, (I'm working with OpenCV in Python 2.7 on Windows.) I had luck to convert a PIL image to an OpenCV image with the algorithm on

Switching among OpenCV, Tensorflow and Pillow? Wait!!!, After opening the image, we convert the image from BGR to RGB. This type of decoding is inaccurate and so to make it the same as OpenCV, we need to decode it by using Let's see the differences with PIL and OpenCV. So OpenCV version is equal to pilimage.convert luminosity, and that is going to return to us an array. Now, let's just write that out to a file. So open_cv_version.save, msirecruitment.png. Now that the conversion of format is done, let's try reading this back into OpenCV. So OpenCV image equals cv.imread and bring out the png.

libjpeg version may by different

Convert the original .jpg image to .bmp image

ffmpeg -i 1.jpg 1.bmp

than the opencv output and PIL output will be the same

Incorrect Grayscale Conversion � Issue #3800 � python-pillow/Pillow , Incorrect Grayscale Conversion #3800. Closed OpenCV Output (verified correct) from PIL import Image import cv2 from argparse import� I want to mimic behavior of PIL.Image.resize(,interpolation=PIL.Image.ANTIALIAS). What I would like to expect, is to have matching pixel vales (~99.9% exact match should be fine) . How can I do this with OpenCV ? I tried Opencv resize with available interpolation methods (including LANCZOS4) , but result does differ from the one I got from PIL.

Since OpenCV reads the image in as BGR format with cv2.imread() we need to convert it back to RGB before giving it to PIL.

Here is an example of reading an image with OpenCv and without change saving it with PIL:

image = cv2.imread('test.jpg')
pil_img = PIL.Image.fromarray(image)
pil_img.save('pil_img.jpg', 'JPEG')

The 'test.jpg' Image: enter image description here

The 'pil_img.jpg' Image: enter image description here

To correct this we need to use cv2.cvtColor to change the image to RGB

correct_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
pil_img = PIL.Image.fromarray(correct_img)
pil_img.save('pil_correct_img.jpg', 'JPEG')

Result: enter image description here

Convert BGR and RGB with Python, OpenCV (cvtColor), When the image file is read with the OpenCV function imread(), the order of as a correct image even if it is saved after being converted to a PIL. to RGB and saved with OpenCV imwrite() , it will be an incorrect color image. What went wrong? Well, this difference is arising from the fact that OpenCV, by default, uses integer accurate decompression of the JPEG image. In contrast, TensorFlow uses Discrete Cosine Transform as default. This type of decoding is inaccurate and so to make it the same as OpenCV, we need to decode it by using integer accurate decompression.

Python: Converting from PIL to OpenCV 2 Image Formats, The conversion from PIL to OpenCV is done with the handy NumPy method " numpy.array" which PIL2Ipl alternative/replacement/not working An accurate conversion to CMYK for printing purposes is not trivial and requires calibrating the printer and measuring CMYK patches on a test sheet, among other things. There is no meaning for CMYK colors that is as ubiquitous as sRGB is for RGB, as illustrated by the International Color Consortium's page of CMYK characterization data.

Importing Image Data into NumPy Arrays, Loading and displaying an image using Matplotlib, OpenCV and Keras API with Pillow from PIL import Image # Open the image form working NumPy uses the asarray() class to convert PIL images into NumPy arrays. The output RGB components of a pixel are interpolated from 1, 2, or 4 neighbors of the pixel having the same color. There are several modifications of the above pattern that can be achieved by shifting the pattern one pixel left and/or one pixel up.

PIL vs Opencv, Let's compare Pillow and OpenCV python libraries on image loading and some basic I asked myself after reading the PyTorch documentation on image transformation. -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno- comment vfdev•3 years ago. 0. Correct! Thanks! Reply � Tuatini GODARD•3 years ago. Prev Tutorial: Camera calibration with square chessboard Next Tutorial: Real Time pose estimation of a textured object Cameras have been around for a long-long time. However, with the introduction of the cheap pinhole cameras in the late 20th century, they became a common occurrence in our everyday life.

Comments
  • This is not what was asked. The question is why OpenCV decodes one pixel as [108 118 112] while PIL/Pillow decodes same pixel as s [104 118 112]. It's not the issue of color order.