0

When I load an image via PIL in Python3, save it and then load it again, I get different values in Ubuntu 18.04.5 vs Ubuntu 20.04.1. I understand that re-saving an image will alter the values due to compression but why is that alteration different in Ubuntu 18.04? Every other OS I tried (Ubuntu 20.04+, Fedora 33, Windows 10 20H2) give one value but Ubuntu 18.04 differs even when there was the same version of pillow/PIL, numpy and python in all the above mentioned operating systems.

Loading an Image

img = Image.open('Sp_D_CNN_A_art0024_ani0032_0268.jpg')
np.sum(np.array(img))
OUTPUT : 28586794    (Same for all the OS)

Saving and loading the duplicate image

img.save('temp.jpg', 'JPEG')
tempimg = Image.open('temp.jpg')
np.sum(np.array(tempimg))
OUTPUT : 28581417    (for Ubuntu 18.04.5 and CentOS 8.2.2004)
         28582606    (for Ubuntu 20.04+, Fedora 33, Windows 10 20H2)

Now, the difference here might look very small but the problem is that after further processing by my Error Level Analysis algorithm the difference becomes huge and as I trained my segmentation model on Google Colab (which uses Ubuntu 18.04.5 in its runtime) the generated mask comes out to be very inaccurate in Ubuntu 20.04+, Fedora 33, Windows 10 20H2.

Why is that happening and how can I fix it?

1
  • 18.04 to 20.04 means two years of improvements and changes to the software, old bugs fixed, new bugs introduced. Great question to ask the PIL upstream developers.
    – user535733
    Commented Nov 28, 2020 at 17:00

1 Answer 1

0

The culprit behind it is the underlying libjpeg version used by the pillow (PIL) library. Although both, Ubuntu 18.04.5 and Ubuntu 20.04.1 has the same libjpeg package, the pillow version installed by default on Ubuntu 18.04.5 has it's own binaries. So a solution for this is to remove current pillow and reinstall it via downloading and building it from source so that it guaranteedly uses the system's libjpeg.

First remove current pillow module

python3 -m pip uninstall Pillow

Then just install the dependency packages listed here: https://pillow.readthedocs.io/en/stable/installation.html#building-on-linux

And then run:

python3 -m pip install Pillow --no-binary :all:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .