简体   繁体   English

图像数组的维度是 3D 而不是 Python 课程中的二维

[英]The dimension of the array of an image is 3D not 2D as it is in the Python course

In my python course, the instructor uploads a greyscale picture of himself and reads it on Python with the following code:在我的python课程中,导师上传一张自己的灰度图,在Python上读取,代码如下:


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.当我编写代码并运行我自己的图像时,使用完全相同的扩展名“tiff”,我得到一个 3 维数组。 I was told it's because my image was colored and so the third entry is for RBG.有人告诉我这是因为我的图像是彩色的,所以第三个条目是 RBG。 So I used a greyscale photo just like he did but I still obtain a 3D array, why?所以我像他一样使用了一张灰度照片,但我仍然得到一个 3D 数组,为什么?

Any help is greatly appreciated, thank you非常感谢任何帮助,谢谢

EDIT编辑

For extra clarity, the array I get for my greyscale image with tiff extension is为了更加清晰,我为带有 tiff 扩展名的灰度图像获得的数组是

(3088, 2316, 4)

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM