Numpy 数组转 PIL 图像格式

Ste*_*zzi 2 python numpy image python-imaging-library

我正在尝试将图像从 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)
Run Code Online (Sandbox Code Playgroud)

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

任何帮助是极大的赞赏!

AGN*_*zer 5

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

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)
Run Code Online (Sandbox Code Playgroud)