[Python] After converting an image from 0-255 pixel range to 0-1 pixel rang, the two images...

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am making sample images using Numpy and Pillow Python libraries. I made 2 images as I describe below:

    For the first image, I used np.random.randint(0, 255) to make a 3D-array to make the pixels digits of an image (digits between 0 and 255). Then I converted this array to an actual image using PIL.Image.fromarray().

    For the second image, I just divided the digits of the 3D array by 255 so that the pixel digits became between 0 and 1, and then I did the PIL.Image.fromarray() again.

    I was expecting the two images to be the same but when I try to plot them, they are very different. Can anyone tell me what I am doing wrong?

    Here is the code and the picture of the resulting plots:

    import numpy as np
    import matplotlib.pyplot as plt
    import PIL

    img_array_1 = np.random.randint(low=0, high=255, size=(16, 16, 3))
    img_0_255 = PIL.Image.fromarray(img_array_1, 'RGB')

    img_array_2 = (img_array_1 / 255)
    img_0_1 = PIL.Image.fromarray(img_array_2, 'RGB')

    plt.subplot(1, 2, 1)
    plt.title("pixels between 1 and 255")
    plt.imshow(img_0_255)

    plt.subplot(1, 2, 2)
    plt.title("pixels between 0 and 1")
    plt.imshow(img_0_1)
    plt.show()


    [​IMG]

    Continue reading...

Compartilhe esta Página