[Python] Error when converting image to array and back to image in python, image is erased

Discussão em 'Python' iniciado por Stack, Março 10, 2025.

  1. Stack

    Stack Membro Participativo

    When I run this code, from original image (blue ring attached), at first show I see circle but after if I convert to RGB and do any manipulation(I tried selecting different columns, etc.), I always get a straight line (hopefully attached here too). Eventually I would like to be able to manipulate RGB channels, but for now just display every color channel as same matrix as original blue channel.

    [[​IMG]

    Code is:

    from PIL import Image
    import matplotlib.pyplot as plt;
    import numpy as np
    import cv2 as cv

    img = Image.open(r"/Users/gadlicht/Downloads/test.jpg") # creating image object
    img1 = img.convert("RGB") # using convert method for img1
    img1.show()


    def magColBlue(x,blurAmount):
    x=np.array(x)
    print("x first shape is ", np.shape(x))
    x=x[:,:,0]
    print("x 2nd shape is ", np.shape(x))
    print(x)
    return x



    blueAmount =magColBlue(img1,0)
    print("blue shape",np.shape(blueAmount))
    imgBlue = Image.fromarray(np.asarray([blueAmount,blueAmount,blueAmount]), 'RGB')
    imgBlue.show()

    Continue reading...

Compartilhe esta Página