Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pillow's JPEG decode result different from OpenCV #6047

Closed
lyksdu opened this issue Feb 13, 2022 · 4 comments
Closed

Pillow's JPEG decode result different from OpenCV #6047

lyksdu opened this issue Feb 13, 2022 · 4 comments
Labels

Comments

@lyksdu
Copy link

lyksdu commented Feb 13, 2022

What did you do?

hey, for some reason, I have to do something about Image decoding. And the conclusion is, Pillow's JPEG decode result different from OpenCV in some cases.
Code:

with open("img_data/test.jpeg", "rb") as fp:
    data= fp.read()
def PIL_load():
    a = np.asarray(Image.open(io.BytesIO(data), ), np.uint8)
    print(a.sum())

def cv2_load():
    b = cv2.imdecode(
                    np.frombuffer(
                        data, np.uint8),
                    cv2.IMREAD_COLOR) 
    print(b.sum())

I find a blog talking about this, but it seems nothing changed after half a year.
link

What did you expect to happen?

I think the output of the two functions is the same, but I got different.

What actually happened?

>>> for PIL_load:        226343825
>>> for opencv_load: 226343957

What are your OS, Python and Pillow versions?

  • OS: Debian 10
  • Python: 3.9
  • Pillow: 9.0

I think this can be reproduced in any jpeg image. I will upload the test image, if necessary.

with open("img_data/test.jpeg", "rb") as fp:
    data= fp.read()
def PIL_load():
    a = np.asarray(Image.open(io.BytesIO(data), ), np.uint8)
    print(a.sum())

def cv2_load():
    b = cv2.imdecode(
                    np.frombuffer(
                        data, np.uint8),
                    cv2.IMREAD_COLOR) 
    print(b.sum())
@radarhere radarhere added the JPEG label Feb 13, 2022
@radarhere
Copy link
Member

radarhere commented Feb 13, 2022

Using one of our test images,

import numpy as np
from PIL import Image
print("Pillow", np.asarray(Image.open("hopper.jpg")).sum())

import cv2
print("OpenCV", cv2.imread("hopper.jpg").sum())

I actually get the same result for them, 4345064. If you run my code over our test image, do the results match for you on your machine as well? Perhaps you do need to upload your image?

@lyksdu
Copy link
Author

lyksdu commented Feb 13, 2022

@radarhere eh, I upgrade my pillow version from 9.0.0.a to 9.0.1, and then this error has been fixed. maybe I have installed an alpha version of Pillow ...

new my version 
Pillow                            9.0.1  # I forget the version before
Pillow-SIMD                       9.0.0.post1 

And I reproduce the problem on version 8.0. On Pillow 8.0.0, hopper.jpg is different.

one version

Pillow                            8.0.0
Pillow-SIMD                       9.0.0.post1

I get

Pillow 4345114
OpenCV 4345064

@nulano
Copy link
Contributor

nulano commented Feb 13, 2022

Pillow 9.0.0 changed to compiling with libjpegturbo instead of libjpeg on all platforms (not just Windows): python-pillow/pillow-wheels#237

It has been previously reported that results with libjpeg are different, but this is to be expected: #3833 (comment)

@radarhere
Copy link
Member

Thanks all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants