admin管理员组文章数量:1323225
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!
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!
Share Improve this question asked Jan 18 at 1:08 k3s-s5lk3s-s5l 15 bronze badges1 Answer
Reset to default 0Meanwhile I resolved the issue with this function:
import numpy as np
def pil_to_tensor(images):
images = np.array(images)
images = torch.from_numpy(images.transpose(2, 0, 1))
return images
And the two code blocks including saving the image can be deleted. Instead calling the function:
pil_image = pil_to_tensor(im)
版权声明:本文标题:python - Turn Grayscale mask-image into RGB while changing one grayscale into specific color - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742085478a2419949.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论