[Python] Different Pillow versions yielding different grayscale values for the same image

Discussão em 'Python' iniciado por Stack, Junho 23, 2025.

  1. Stack

    Stack Membro Participativo

    While converting PNG (RGB) to grayscale using two different versions of Pillow and Python, different grayscale values were observed.

    I used Pillow version 5.4.1 on Python 2.7.18, and Pillow version 11.2.1 on Python 3.12.0

    On checking the code on Github it mentions in the comment that for grayscale conversion following formula is used:

    [​IMG]

    Link: https://github.com/python-pillow/Pillow/blob/11.2.x/src/PIL/Image.py

    I need the same grayscale values on Python 3.12.0 as that in the Python 2.7.18. Pillow version 5.4.1 is not supported in Python 3.12.0.

    Pillow uses L = R * 299/1000 + G * 587/1000 + B * 114/1000

    But it does not yield same grayscale pixel value for two different version of Pillow (5.4.1 and 11.2.1)

    I used the formula itself instead of Pillow for grayscale conversion L = floor(R * 0.299 + G * 0.587 + B * 0.114) in Python 3.12.0

    But I encountered the following problem: For pixel value 4,4,4 Pillow 5.4.1 converted it to grayscale value of 4 But the formula yielded the grayscale value of 3

    How can be the result of Pillow 5.4.1 replicated in Python 3.12.0 for grayscale conversion?

    Note: Since I'm using a PNG, when image is first read, the pixel values are same in case of Pillow 5.4.1 and Pillow 11.2.1

    Continue reading...

Compartilhe esta Página