Home > database >  The dimension of the array of an image is 3D not 2D as it is in the Python course
The dimension of the array of an image is 3D not 2D as it is in the Python course

Time:09-29

In my python course, the instructor uploads a greyscale picture of himself and reads it on Python with the following code:


import numpy as np
import math
from PIL import Image
from IPython.display import display

im = Image.open("chris.tiff")
array = np.array(im)

print(array.shape)

and he gets

(200,200)

When I write the code and run my own image, with the exact same extension "tiff", I get a 3-dimensional array. I was told it's because my image was colored and so the third entry is for RBG. So I used a greyscale photo just like he did but I still obtain a 3D array, why?

Any help is greatly appreciated, thank you

EDIT

For extra clarity, the array I get for my greyscale image with tiff extension is

(3088, 2316, 4)

CodePudding user response:

Your photo appears to be grey, but actually, it has the three channels based on the posted shape.

So, you need to convert it to greyscale using the following line:

im = Image.open("chris.tiff").convert('L')
  • Related