[Python] How to convert a 3d RGB NumPy array to float without changing the image?

Discussão em 'Python' iniciado por Stack, Julho 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to save an image that is in RGB pixel form. For whatever reason, when I call matplotlib.pyplot.imshow, the image is displayed properly.

    However, when I call matplotlib.pyplot.imsave, I get an error that the array has to be in float. When I decided to change all the values to float, the image is completely modified. It makes no sense to me.

    def display_image(pixels, name=""):
    if name:
    plt.imsave(name, array)
    plt.imshow(array)

    pixels = im.getdata()
    pixelMap = im.load()
    npImage = np.array(pixels).reshape(671, 450, 3)
    display_image(npImage) ## great!

    # things that I tried
    display_image(npImage, "image.jpg") ## error, must be floats.

    # changes the images
    pixels = list(pixels) ## pixels var originally imagecore class
    for index in range(len(pixels)):
    pixels[index] = (float(pixels[index][0]), float(pixels[index][1]), float(pixels[index][2]))
    display_image(npImage, "monaLisa.jpg") ## works but incorrect image

    Continue reading...

Compartilhe esta Página