Skip to content

Instantly share code, notes, and snippets.

@ritvikmath
Created December 6, 2019 20:58
Show Gist options
  • Select an option

  • Save ritvikmath/57e1a11669aa03b7ff25b748165bef5e to your computer and use it in GitHub Desktop.

Select an option

Save ritvikmath/57e1a11669aa03b7ff25b748165bef5e to your computer and use it in GitHub Desktop.
#create a copy of my image
copyImg = img.copy()
#get the average of each pixel
#we need axis=2 so that we take averages accross the 3rd axis (the color axis) rather than accross the rows or columns
average_pixel_values = np.mean(copyImg, axis=2, keepdims=True)
#create the grey image by "stacking" three copies of these grey values together
greyImage = np.concatenate([average_pixel_values]*3, axis=2)
#make sure to cast the final image back to integers
greyImage = greyImage.astype(int)
#show the final image
plt.imshow(greyImage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment