So I wrote this little code to try to convert RGB
imge to Grayscale
taken from this accepted answer. The problem, is that it shows a cryptic image with no likeness to original even when I am just re-creating the original. What do you think is the problem and how should we go about solving it? Also then I want to convert it to Grayscale
as per the given array b
.
Here's my code:
import matplotlib.image as img
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
a = img.imread('hCeFA.png')
a = a * 255 #Matplotlib gives float values between 0-1
b = a * np.array([0.3, 0.59, 0.11])
b = np.sum(b, axis = 2) / 3 #Grayscale conversion
img1 = Image.fromarray(a, 'RGB')
img1.save('my.png')
img1.show() #Gives a cryptic image
plt.imshow(a/255, interpolation='nearest') #Works fine
plt.show()