Skip to content

Pillow's JPEG decode result different from OpenCV #6047

@lyksdu

Description

@lyksdu

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())

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions