How to generate a color using Torch

Hello Team,

Have a task to pass a Target color ,How to define a color in torch ?
right now i am using the same .

Target_color = torch.randn(1,256).to(device)

But i am not sure what color is this

so my task is

Example - Passing a color red

Pass color red to Torch Tensor

Please let me know on the same .

I don’t know what the actual use case is, but take a look at the RGB color space and how a color is defined in it (i.e. via 3 uint8 values). Then create a tensor with the desired color by assigning the R, G, and B value to it.

hello patrick,

For Example this is the code i am using generating using numpy

import numpy as np
import matplotlib.pyplot as plt

Create a blank 300x300 red image

image = np.zeros((200, 200, 3), np.uint8)

Fill image with red color(set each pixel to red)

image[:] = (255, 0, 0)

It is a red color

Now i want to use this color information in torch ,How can i do that ?

If you want to use the numpy array as a tensor, you could use tensor = torch.from_numpy(image). Alternatively, if you want to create the same “red tensor” this should work:

image = torch.zeros(200, 200, 3, dtype=torch.uint8)
image[:, :, 0] = 255

Thanks a Lot Patrick this worked for me .