i am using below code to reshape my image. It's working for RGB image. But, it's not working for grey scale image.
from PIL import Image
import numpy as np
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image).reshape((im_height, im_width, 3)).astype(np.uint8)
image_path="color.jpg"
image = Image.open(image_path)
image_np = load_image_into_numpy_array(image)
image.close()
This is working image color.jpg
This is not working image grey.jpg
Both images shapes' are same.
image.size
(714, 714)
When I print image i found a differnce.
Working image print(image)
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=714x714 at 0x7F95DB4D4BA8>
not working image print(image)
<PIL.JpegImagePlugin.JpegImageFile image mode=L size=714x714 at 0x7F32B5430BA8>
- how to fix the issue?
- Is this because of mode changes?
Any help would be appreciable.
Error:
Traceback (most recent call last):
File "checker.py", line 11, in <module>
image_np = load_image_into_numpy_array(image)
File "checker.py", line 5, in load_image_into_numpy_array
return np.array(image).reshape((im_height, im_width, 3)).astype(np.uint8)
ValueError: cannot reshape array of size 509796 into shape (714,714,3)