Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将图像作为2D数组导入python_Python_Arrays_Numpy_Python Imaging Library - Fatal编程技术网

将图像作为2D数组导入python

将图像作为2D数组导入python,python,arrays,numpy,python-imaging-library,Python,Arrays,Numpy,Python Imaging Library,我只是想知道有没有一种方法可以在python中使用numpy和PIL导入图像,使其成为2D数组?此外,如果我有黑白图像,是否可以将黑色设置为1,将白色设置为0 目前我正在使用: temp=np.asarray(Image.open("test.jpg")) frames[i] = temp #frames is a 3D array 有了这个,我得到了一个错误: ValueError:操作数无法与形状(700600)(600700,3)一起广播 我是python新手,但据我所知,这意味着基本上

我只是想知道有没有一种方法可以在python中使用numpy和PIL导入图像,使其成为2D数组?此外,如果我有黑白图像,是否可以将黑色设置为1,将白色设置为0

目前我正在使用:

temp=np.asarray(Image.open("test.jpg"))
frames[i] = temp #frames is a 3D array
有了这个,我得到了一个错误:

ValueError:操作数无法与形状(700600)(600700,3)一起广播


我是python新手,但据我所知,这意味着基本上temp是一个3D数组,我将其分配给2D数组?

我不是专家,但我可以想出一些方法,不确定您想要实现什么,因此您可能不喜欢我的解决方案:

from PIL import Image
from numpy import*

temp=asarray(Image.open('test.jpg'))
for j in temp:
    new_temp = asarray([[i[0],i[1]] for i in j]) # new_temp gets the two first pixel values
此外,还可以使用.resize():

如果将图片转换为黑白,阵列将自动变为2D:

from PIL import Image
from numpy import*

temp=Image.open('THIS.bmp')
temp=temp.convert('1')      # Convert to black&white
A = array(temp)             # Creates an array, white pixels==True and black pixels==False
new_A=empty((A.shape[0],A.shape[1]),None)    #New array with same size as A

for i in range(len(A)):
    for j in range(len(A[i])):
        if A[i][j]==True:
            new_A[i][j]=0
        else:
            new_A[i][j]=1

是的,这就是它的意思。您的x、y轴似乎也发生了反转。
from PIL import Image
from numpy import*

temp=Image.open('THIS.bmp')
temp=temp.convert('1')      # Convert to black&white
A = array(temp)             # Creates an array, white pixels==True and black pixels==False
new_A=empty((A.shape[0],A.shape[1]),None)    #New array with same size as A

for i in range(len(A)):
    for j in range(len(A[i])):
        if A[i][j]==True:
            new_A[i][j]=0
        else:
            new_A[i][j]=1