这篇教程【PyTorch】生成一张随机噪声图片写得很实用,希望能帮到您。 【PyTorch】生成一张随机噪声图片 代码 from torchvision.transforms import ToPILImage import torch
def random_noise(nc, width, height): '''Generator a random noise image from tensor.
If nc is 1, the Grayscale image will be created. If nc is 3, the RGB image will be generated.
Args: nc (int): (1 or 3) number of channels. width (int): width of output image. height (int): height of output image. Returns: PIL Image. ''' img = torch.rand(nc, width, height) img = ToPILImage()(img) return img
if __name__ == '__main__': random_noise(3, 256, 256).save('random-noise.jpg')
结果 彩色图 灰度图 参考 https://pytorch.org/docs/stable/generated/torch.rand.html#torch-rand https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.ToPILImage
原文链接:https://blog.csdn.net/qq_42951560/article/details/116724901 返回列表 Python生成一张随机噪声图片(Numpy+Pillow) |