How about doing it with Pillow:
from PIL import Image
img = Image.open('image.png').convert('L')
img.save('greyscale.png')
If an alpha (transparency) channel is present in the input image and should be preserved, use mode LA:
img = Image.open('image.png').convert('LA')
Using matplotlib and the formula
Y' = 0.2989 R + 0.5870 G + 0.1140 B
you could do:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
img = mpimg.imread('image.png')
gray = rgb2gray(img)
plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()
Answer from unutbu on Stack OverflowHow about doing it with Pillow:
from PIL import Image
img = Image.open('image.png').convert('L')
img.save('greyscale.png')
If an alpha (transparency) channel is present in the input image and should be preserved, use mode LA:
img = Image.open('image.png').convert('LA')
Using matplotlib and the formula
Y' = 0.2989 R + 0.5870 G + 0.1140 B
you could do:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
img = mpimg.imread('image.png')
gray = rgb2gray(img)
plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()
You can also use scikit-image, which provides some functions to convert an image in ndarray, like rgb2gray.
from skimage import color
from skimage import io
img = color.rgb2gray(io.imread('image.png'))
Notes: The weights used in this conversion are calibrated for contemporary CRT phosphors: Y = 0.2125 R + 0.7154 G + 0.0721 B
Alternatively, you can read image in grayscale by:
from skimage import io
img = io.imread('image.png', as_gray=True)
python - Converting an image to grayscale using numpy - Stack Overflow
numpy - Turn a image to grayscale in python - Stack Overflow
python - How to make grayscale image using numpy and matplotlib - Stack Overflow
python - How to change an image to grayscale represented as a NumPy array - Stack Overflow
Here is a working code:
def grayConversion(image):
grayValue = 0.07 * image[:,:,2] + 0.72 * image[:,:,1] + 0.21 * image[:,:,0]
gray_img = grayValue.astype(np.uint8)
return gray_img
orig = cv2.imread(r'C:\Users\Jackson\Desktop\drum.png', 1)
g = grayConversion(orig)
cv2.imshow("Original", orig)
cv2.imshow("GrayScale", g)
cv2.waitKey(0)
cv2.destroyAllWindows()
You can use a dot product:
gray_image = image.dot([0.07, 0.72, 0.21])
Or even just do the whole operation manually:
b = image[..., 0]
g = image[..., 1]
r = image[..., 2]
gray_image = 0.21 * r + 0.72 * g + 0.07 * b
Don't forget to convert back to 0-255:
gray_image = np.min(gray_image, 255).astype(np.uint8)
Two issues. Firstly, you are not converting to greyscale. Secondly, the whole point of numpy is to vectorise and avoid for loops because they are slow.
So starting with this image:

You want something like this:
#!/usr/local/bin/python3
import numpy as np
from PIL import Image
# Load coins and convert to greyscale
grey = np.array(Image.open('coins.png').convert('L'))
# Threshold at 128
thresholded=((grey>128)*255).astype(np.uint8)
# Save
Image.fromarray(thresholded).save('result.png')

Sruthi V is right. enumerate() returns a tuple.
So instead of
if item < 128
it should be
if item[1] < 128
Or you could just remove enumerate() entirely if you aren't using it. It seems it will work fine without it.
I am trying to use getdata() on a 500x500 image. As far as i understand, this returns a 750000 entry array in groups of three corresponding to the RGB values of each cell. I want to change these to a single value (in grayscale) and arrange it in a 500x500 matrix to mimic the image. Here is my code
import sys
import numpy as np
from descent import descent_func
from PIL import Image
myimage = Image.open("Spiral.png")
#myimage.show()
b = np.array(list(myimage.getdata()))
b = b.reshape(1,750000)
c = np.empty((500,500))
k = 0
for i in range(0,499):
for j in range(0,499):
c[i][j] = b[0][k]
k = k + 3
print(c)
img = Image.fromarray(c)
img.show()I figured since my input drawing in strictly black and white, i could just use the R value from the RGB for my greyscale value, but this produces a shifted version of the input image. I'm very new to python so not quite sure how to proceed. Thanks!