Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 如何将数组的二进制numpy列表转换为图像?_Python_Arrays_Image_Numpy_Image Processing - Fatal编程技术网

Python 如何将数组的二进制numpy列表转换为图像?

Python 如何将数组的二进制numpy列表转换为图像?,python,arrays,image,numpy,image-processing,Python,Arrays,Image,Numpy,Image Processing,作为第一步,我希望与0不同的像素为白色,而0像素为黑色。我如何将“无0”值全部转换为1: [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

作为第一步,我希望与0不同的像素为白色,而0像素为黑色。我如何将“无0”值全部转换为1:

[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 1 1 1 0 0 0 0 0 1 1 0 0 3 3 0 0 0 4 4 0 0 0 5 5 5 5 0 0 2 2 2 2 2 0 2 2 2 2 2 0 0 0 6 6 6 6 6 6 0 6 6 6 6]
 [0 1 1 0 0 0 0 0 0 0 0 0 0 3 3 0 0 0 4 4 0 0 5 5 5 5 5 5 0 2 2 2 2 2 2 2 2 2 2 2 2 0 0 6 6 6 6 6 6 6 6 6 6 6]
 [1 1 1 0 0 0 0 0 0 0 0 0 0 3 3 0 0 0 4 4 0 5 5 5 0 0 5 5 5 0 2 2 0 0 2 2 0 0 0 2 2 0 0 6 6 0 0 6 6 6 0 0 6 6]
 [1 1 1 0 0 0 0 0 0 0 0 0 0 3 3 0 0 0 4 4 0 5 5 5 5 0 0 0 0 0 2 2 0 2 2 2 0 0 0 2 2 2 0 6 6 0 0 0 6 6 0 0 6 6]
 [1 1 1 0 0 0 0 0 0 0 0 0 0 3 3 0 0 0 4 4 0 0 5 5 5 5 5 5 0 0 2 2 0 2 2 2 0 0 0 2 2 2 0 6 6 0 0 0 6 6 0 0 6 6]
 [0 1 1 0 0 0 0 0 0 7 0 0 0 3 3 0 0 0 4 4 0 0 0 0 5 5 5 5 5 0 2 2 0 2 2 2 0 0 0 2 2 2 0 6 6 0 0 0 6 6 0 0 6 6]]
然后我想将1和0的数组列表转换为图像,我尝试了:

binary_transform = np.array(labels).astype(bool).astype(int)
Image.fromarray
的文档可以在这里找到

它不起作用,然后我尝试了以下方法:

from PIL import Image

img = Image.fromarray(binary_transform, '1')
img.save('image.png')
引用docs
'L'
是为了灰度,而我想要二进制,但我没有看到二进制选项,即docs


我得到了这个错误
ValueError:bitdepth(64)必须是一个正整数,虽然你没有明确地说,但你说的“作为第一步…”这一事实让我认为你正在走向灰度调色板图像:

import png

png.from_array(binary_transform, 'L').save('image.png')

请注意,为了便于显示,我已经调整了图像的大小并进行了对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0,6,6,0,0,0,6,6,0,0,6,6],
 [0,1,1,0,0,0,0,0,0,7,0,0,0,3,3,0,0,0,4,4,0,0,0,0,5,5,5,5,5,0,2,2,0,2,2,2,0,0,0,2,2,2,0,6,6,0,0,0,6,6,0,0,6,6]]
binary_transform = np.array(labels).astype(np.uint8)
img = Image.fromarray(binary_transform, 'P')
img.save('image.png')

如果我没弄错的话,你希望图像是二值的,也就是说,只有黑白,没有灰色。如果是这样,OpenCV就是您的朋友:

binary_transform = np.array(labels).astype(np.uint8)
binary_transform[binary_transform>0] = 255
img = Image.fromarray(binary_transform, 'L')
img.save('image.png')
当然,PIL也可以工作,您只需要调整非零值

import cv2
import numpy as np

binary_transform = np.array(labels).astype(np.uint8)

_,thresh_img = cv2.threshold(binary_transform, 0, 255, cv2.THRESH_BINARY)

cv2.imwrite('image.png', thresh_img)
其他答案(都很好!)使用OpenCV或PIL。下面是如何使用创建图像的方法,这是我编写的一个小库,用于从numpy数组创建PNG文件

首先,以下是我的示例数据:

binary_transform = np.array(labels).astype(np.uint8)
binary_transform[binary_transform > 0] = 255

img = Image.fromarray(binary_transform, 'L')
img.save('image.png')
使用
numpngw.write\u png()
创建图像:


这是图像:

实际上不是灰度图像,它是应用于灰度图像的连接组件函数的输出(
cv2.connectedComponents
)我猜它是连接组件标签:-)很酷,用两种不同的方法来做的+1!
In [173]: x
Out[173]: 
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 1, 1, 1, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 1, 1, 1, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 1, 1, 1, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 1, 1, 1, 1, 0, 3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