0

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()
DuttaA
  • 903
  • 4
  • 10
  • 24
  • What makes you think `b = a * np.array([0.3, 0.59, 0.11])` is grayscale conversion, and why are you doing nothing with `b`? – user2357112 Jul 25 '18 at 16:46
  • @user2357112 https://www.tutorialspoint.com/dip/grayscale_to_rgb_conversion.htm – DuttaA Jul 25 '18 at 16:47
  • That tutorial says to multiply the individual color channel values by those factors and *add the results together* to determine the grayscale value. – user2357112 Jul 25 '18 at 16:50
  • @user2357112 if the original image shows cryptic values how can grayscale image have any different outcome, i have tried that already and it has given me the same outcome..anyways i updated the question – DuttaA Jul 25 '18 at 16:51
  • Again, why are you not actually using `b` for anything? – user2357112 Jul 25 '18 at 16:51
  • @user2357112 like i said i am unable to recreate the 'original' image...and so i am asking how to do that and then convert it into grayscale – DuttaA Jul 25 '18 at 16:53

1 Answers1

1

Better Read the image with the 'I'. Like imread('imageName.imgFormat',"I") That will resolve the issue. It will read image in uint8 format, which i think you desire.

lifeisshubh
  • 513
  • 1
  • 5
  • 27