Stefano Pozzi
Stefano Pozzi

Reputation: 599

Numpy array to PIL image format

I'm trying to convert an image from a numpy array format to a PIL one. This is my code:

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)

The input to this method is a PIL image. This method should add Gaussian noise to the image and return it as a PIL image once more.

Any help is greatly appreciated!

Upvotes: 4

Views: 9718

Answers (1)

AGN Gazer
AGN Gazer

Reputation: 8378

In my comments I meant that you do something like this:

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)

Upvotes: 7

Related Questions