Get range of value in tensor

i have a tensor which shape is [4,1,224,224]. This is a 4 grayscale image batch. I want to get certain range values of each image. For example, the image grayscale range is 0 to 255, I want to get 30 to 40 values from the image and other pixels should be zero. is there a good way to do it?

torch.where should work:

x = torch.randint(0, 256, (4, 1, 224, 224))
print(x.min(), x.max())
> tensor(0) tensor(255)
out = torch.where((30<=x) & (x<=40), x, 0)
print(out.unique())
> tensor([ 0, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40])