Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 802
Stefano Pozzi Avatar asked Oct 25 '25 17:10

Stefano Pozzi


1 Answers

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)
like image 88
AGN Gazer Avatar answered Oct 28 '25 09:10

AGN Gazer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!