Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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
Python PIL图像打开将白色像素转换为黑色像素_Python_Python 3.x_Python Imaging Library - Fatal编程技术网

Python PIL图像打开将白色像素转换为黑色像素

Python PIL图像打开将白色像素转换为黑色像素,python,python-3.x,python-imaging-library,Python,Python 3.x,Python Imaging Library,当我打开一个带有枕头的全白色文件(来自PIL导入图像),然后获取所有像素的颜色时,您应该会看到类似于[255,255,255,255,255,255]…的内容,但我只看到[0,0,0,0,0]…,代码如下: from PIL import Image image = Image.open("index.png", "r") pixels = list(image.getdata()) print(pixels) 您的代码不会将白色像素值转换为黑色像素。它

当我打开一个带有枕头的全白色文件(
来自PIL导入图像),然后获取所有像素的颜色时,您应该会看到类似于
[255,255,255,255,255,255]…
的内容,但我只看到
[0,0,0,0,0]…
,代码如下:

from PIL import Image
image = Image.open("index.png", "r")
pixels = list(image.getdata())
print(pixels)

您的代码不会将白色像素值转换为黑色像素。它以不同的方式表示像素值。我们可以检查它是否使用RGB颜色域将白色像素值转换为黑色像素。代码如下所示:

from PIL import Image
import numpy as np
img = Image.open("index.png") # open colour image


imgRgb = img.convert('RGB')
pixels = list(imgRgb.getdata())
width, height = imgRgb.size
pixels = np.asarray([pixels[i * width:(i + 1) * width] for i in range(height)], dtype=int)

如果可能的话,您可以上传此代码中使用的图像吗?它可能由三个通道组成,其中一个通道可能专用于黑白像素值。您可以在此处看到它(以及代码):