1

What I want to do is take an image file that I read in (scipy.misc.imread(file)) and change every individual RGB value to an average of the three values for that pixel.

For example, I can do this on one individual pixel doing something like this:

import numpy as np
from scipy import misc
import matplotlib.pyplot as plt
from skimage import data

img = misc.imread('./path/to/file.jpg')
print(img[200, 200]) #[145 165 155]
print(img[200, 200]) = int(np.sum(img[200, 200])/3) # sets RGB values at img[200, 200] to the average of the RGB values in this case, 155
print(img[200, 200]) # changed to [155 155 155]

However, I'm new to numpy and ndarrays and I want to know how to apply this to every pixel in the image using slicing. Is this possible? I'm having trouble understanding how to iterate over the entire ndarray and reference the appropriate values to sum and set.

Any help is welcome!

1 Answer 1

1

You can take the mean using numpy.

import numpy as np
import matplotlib.pyplot as plt

from scipy import misc

img = misc.imread('baboon.png')
mean_img = img.mean(axis=2)

plt.imshow(mean_img, cmap=plt.cm.gray)
plt.show()

enter image description here enter image description here

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.