Actually while converting jpg format to gray scale we have to use img.convert(‘LA’) instead of img.convert(‘L’).
I read that while reading gray scale image in opencv it gives us 3 layers of gray scale. I tried PNG format still its showing the same error.