4

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!

9
  • 1
    Is there something wrong with your code? What is the question? Commented Jun 12, 2018 at 16:55
  • Re-added information now, don't know why it didn't show up once the question was posted Commented Jun 12, 2018 at 16:57
  • PIL is deprecated, use skimage instead Commented Jun 12, 2018 at 16:57
  • No, the PIL-alternative is Pillow (while skimage is great, it has a different target-group). And the author really should read the close-vote and the first comment and react to those. The problem, whatever it is, is probably related to types (e.g. uint8 image; gaussian is obviously continuous/float_double resulting in a change of types). Hint: there are similar questions here! Commented Jun 12, 2018 at 16:59
  • I would suggest you follow the steps outlined in stackoverflow.com/a/10967471/8033585 except replace colormap step with "adding noise", e.g., add noise, rescale (0-255), convert to uint8, etc. Commented Jun 12, 2018 at 17:01

1 Answer 1

7

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.