首页 文章

Numpy数组到PIL图像格式

提问于
浏览
-1

我正在尝试将图像从numpy数组格式转换为PIL格式 . 这是我的代码:

img = numpy.array(image)
row,col,ch= np.array(img).shape
mean = 0
# var = 0.1
# sigma = var**0.5
gauss = np.random.normal(mean,1,(row,col,ch))
gauss = gauss.reshape(row,col,ch)
noisy = img + gauss
im = Image.fromarray(noisy)

此方法的输入是PIL图像 . 此方法应将高斯噪声添加到图像并再次将其作为PIL图像返回 .

任何帮助是极大的赞赏!

1 回答

  • 2

    在我的评论中,我的意思是你做这样的事情:

    import numpy as np
    from PIL import Image
    
    img = np.array(image)
    mean = 0
    # var = 0.1
    # sigma = var**0.5
    gauss = np.random.normal(mean, 1, img.shape)
    
    # normalize image to range [0,255]
    noisy = img + gauss
    minv = np.amin(noisy)
    maxv = np.amax(noisy)
    noisy = (255 * (noisy - minv) / (maxv - minv)).astype(np.uint8)
    
    im = Image.fromarray(noisy)
    

相关问题