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

Same image has different values once imported, same OS, python version and pillow version but install pip vs conda #5887

Closed
sergedurand opened this issue Dec 14, 2021 · 4 comments
Labels
Anaconda Issues with Anaconda's Pillow JPEG

Comments

@sergedurand
Copy link

sergedurand commented Dec 14, 2021

What did you do?

I imported the same jpg image in two different environment but with the same pillow and python version.

What did you expect to happen?

The imported image to have the same value.

What actually happened?

The pixel values differs.

What are your OS, Python and Pillow versions?

  • OS: Windows 10
  • Python: 3.9.7
  • Pillow: 8.4.0

To reproduce the result, assuming one has conda installed:

conda create -n env_1 python=3.9.7
conda activate env_1
conda install -c conda-forge pillow==8.4.0

Then run:

import PIL.Image

img = PIL.Image.open("tiny_mnist.jpg")
data = img.load()
width, height = img.size
print("Mode : {}".format(img.mode))
for x in range(width):
    for y in range(height):
        cpixel = data[x, y]
        print("pixel ({}, {}): {}".format(x, y, cpixel))

You will get

Mode : RGB
pixel (0, 0): (62, 2, 90)
pixel (0, 1): (58, 0, 84)
pixel (0, 2): (58, 0, 81)
[...]

(Full result in res1.txt)

But doing

conda create -n env_2 python=3.9.7
conda activate env_2
pip install pillow==8.4.0

Then running the same script you will get:

Mode : RGB
pixel (0, 0): (65, 1, 85)
pixel (0, 1): (61, 0, 79)
pixel (0, 2): (61, 0, 76)
[...]

(Full result in res2.txt)

The pixel values are different.

After investigating when installing from conda more dependencies are installed, in particular some libraries related to images.
This is the result of conda env export > env_1.yml

name: env_1
channels:
  - defaults
  - conda-forge
dependencies:
  - ca-certificates=2021.10.8=h5b45459_0
  - certifi=2021.10.8=py39hcbf5309_1
  - freetype=2.10.4=h546665d_1
  - jbig=2.1=h8d14728_2003
  - jpeg=9d=h8ffe710_0
  - lcms2=2.12=h2a16943_0
  - lerc=3.0=h0e60522_0
  - libdeflate=1.8=h8ffe710_0
  - libpng=1.6.37=h1d00b33_2
  - libtiff=4.3.0=hd413186_2
  - libzlib=1.2.11=h8ffe710_1013
  - lz4-c=1.9.3=h8ffe710_1
  - olefile=0.46=pyh9f0ad1d_1
  - openjpeg=2.4.0=hb211442_1
  - openssl=3.0.0=h8ffe710_2
  - pillow=8.4.0=py39h916092e_0
  - pip=21.3.1=pyhd8ed1ab_0
  - python=3.9.7=h900ac77_3_cpython
  - python_abi=3.9=2_cp39
  - setuptools=59.4.0=py39hcbf5309_0
  - sqlite=3.37.0=h8ffe710_0
  - tk=8.6.11=h8ffe710_1
  - tzdata=2021e=he74cb21_0
  - ucrt=10.0.20348.0=h57928b3_0
  - vc=14.2=hb210afc_5
  - vs2015_runtime=14.29.30037=h902a5da_5
  - wheel=0.37.0=pyhd8ed1ab_1
  - wincertstore=0.2=py39hcbf5309_1007
  - xz=5.2.5=h62dcd97_1
  - zlib=1.2.11=h8ffe710_1013
  - zstd=1.5.0=h6255e5f_0

And this is the one for env_2, with pillow installed via pip:

name: env_2
channels:
  - defaults
  - conda-forge
dependencies:
  - ca-certificates=2021.10.26=haa95532_2
  - certifi=2021.10.8=py39haa95532_0
  - openssl=1.1.1l=h2bbff1b_0
  - pip=21.2.4=py39haa95532_0
  - python=3.9.7=h6244533_1
  - setuptools=58.0.4=py39haa95532_0
  - sqlite=3.36.0=h2bbff1b_0
  - tzdata=2021e=hda174b7_0
  - vc=14.2=h21ff451_1
  - vs2015_runtime=14.27.29016=h5e58377_2
  - wheel=0.37.0=pyhd3eb1b0_1
  - wincertstore=0.2=py39haa95532_2
  - pip:
    - pillow==8.4.0

Could you confirm this is the origin of the discrepancy? I don't have this difference on Ubuntu 20.04 despite a similar behaviour of conda and pip.

I guess this is not really a bug but I wanted to raise the issue in case others encounters the situation. A solution to avoid differences is to make sure the exact same environment is used. Also other formats (png, tif, bitmap) seem to be more robust to the change of environments, I only encountered the problem with jpg so far.
Image and full print output:
res1.txt
res2.txt
tiny_mnist

@nulano
Copy link
Contributor

nulano commented Dec 14, 2021

Probably a duplicate of #3833 and others.

JPG is a lossy format and can be decoded differently by different implementations. The two Pillow versions (PyPI and Conda) likely use a different version of jpeglib. Since you are using Windows, the pip version likely uses the latest jpeglib-turbo, while from your post it looks like Conda is using jpeglib version 9d.

@radarhere radarhere added the JPEG label Dec 14, 2021
@sergedurand
Copy link
Author

sergedurand commented Dec 14, 2021

Thanks for the reply. Indeed following the issue you linked I printed print(Image.core.jpeglib_version) and get 8.0 for the pip install and 9.0 for the conda install.

@radarhere
Copy link
Member

Are you happy for this to be closed then?

@sergedurand
Copy link
Author

Yes :)

@aclark4life aclark4life added the Anaconda Issues with Anaconda's Pillow label May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Anaconda Issues with Anaconda's Pillow JPEG
Projects
None yet
Development

No branches or pull requests

4 participants