[Python] Turn Grayscale mask-image into RGB while changing one grayscale into specific color

Discussão em 'Python' iniciado por Stack, Janeiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    While using torch and matplotlib I was able to change the grayscale color #50 e.g. to red. However, only by saving the image inbetween. How can it be solved without saving the image? I am sorry for the simple question, but I am new with torch, torchvision and matplotlib.

    Here is my code, first to save the image:

    import torch
    import matplotlib.pyplot as plt
    from torchvision.io import read_image

    mask = read_image("A_mask.png")

    mask_np = mask.numpy()
    print(mask_np.shape) #(1, 438, 567)
    mask_np[mask_np == 4] = 50
    output_image = torch.tensor(mask_np)

    from torchvision import transforms
    im = transforms.ToPILImage()(output_image).convert("RGB")
    im.save("A-Mask-Colored-By-Hand.png")


    Then to load it:

    from torchvision import transforms

    m = read_image("A-Mask-Colored-By-Hand.png")
    #print(type(m)) # <class 'torch.Tensor'>


    Here is my question, how to convert "im" into expected format, instead reading it. This code block is what I have tried and which is not working: So commenting out this block ... will work but is not optimal by saving and loading again.

    from PIL import Image
    #tensor_image = im.permute(1,2,0) # #
    tensor_image = im
    pil_image = Image.fromarray((tensor_image.numpy() * 255).astype("uint8"))
    # here it stops, throwing an AttributeError: 'Image' object has no attribute 'numpy'


    This code block works:

    m = pil_image
    print(m.shape) #(3, 438, 567)
    m = m.numpy()
    print(m.shape) #(3, 438, 567)

    cgrey = 50 # grey #50 > into red #255
    mask = (m[0]==cgrey) & (m[1]==cgrey) & (m[2]==cgrey)
    m[0][mask] = 255
    m[1][mask] = 0
    m[2][mask] = 0

    output_image = torch.tensor(m)

    from torchvision import transforms
    im = transforms.ToPILImage()(output_image).convert("RGB")
    print(im) # tensor
    im.save("A-Mask-Colored-RGB.png")


    Thanks in advance!

    Continue reading...

Compartilhe esta Página