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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]]

As a first step I want the pixels different than 0 to be white and the 0 pixels to be black.what i did to transform the none 0 values all to 1: 第一步,我希望将不同于0的像素设置为白色,将0像素设置为黑色,然后将非0值全部转换为1:

binary_transform = np.array(labels).astype(bool).astype(int)

and it worked then i want to transform the list of arrays of 1s and 0s to image, what i tried: 它起作用了,然后我想将1和0的数组列表转换为图像,我尝试了什么:

from PIL import Image

img = Image.fromarray(binary_transform, '1')
img.save('image.png')

the docs for Image.fromarray can be found here https://pillow.readthedocs.io/en/3.1.x/reference/Image.html 有关Image.fromarray的文档, Image.fromarray参见https://pillow.readthedocs.io/en/3.1.x/reference/Image.html

It didn't work then i tried the following: 它没有用,然后我尝试了以下方法:

import png

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

Referring to the docs 'L' is for grayscale while i want binary but i didn't see a binary option, the docs https://pythonhosted.org/pypng/png.html 当我想要二进制文件但我没有看到二进制选项时,指'L'是文档'L'是灰度的,文档https://pythonhosted.org/pypng/png.html

and i got this error ValueError: bitdepth (64) must be a positive integer <= 16 我收到此错误ValueError: bitdepth (64) must be a positive integer <= 16

If I understand you right, you want the image to appear binary, ie, just black and white, no grey. 如果我理解正确,则希望图像显示为二进制,即黑白图像,无灰色。 If that's the case, OpenCV is your friend: 如果是这样,OpenCV是您的朋友:

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)

Of course PIL will work as well, you just need to adjust your non-zero values. 当然,PIL也可以正常工作,您只需要调整非零值即可。

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

img = Image.fromarray(binary_transform, 'L')
img.save('image.png')

在此处输入图片说明

Though you don't say that explicitly, the fact that you said "As a first step..." , makes me think you are heading towards a greyscale palette image: 尽管您没有明确地说出来,但是您说的“第一步...”这一事实使我认为您正朝着灰度调色板图像前进:

import numpy as np
from PIL import Image
labels=[[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]]
binary_transform = np.array(labels).astype(np.uint8)
img = Image.fromarray(binary_transform, 'P')
img.save('image.png')

在此处输入图片说明

Note that I have resized and contrast-stretched the image for display purposes. 请注意,为了显示目的,我已经调整了图像的大小并进行了对比拉伸。

If you really only want a true binary, black and white image, use: 如果您真的只想要一个真正的二进制黑白图像,请使用:

binary_transform = np.array(labels).astype(np.uint8)
binary_transform[binary_transform>0] = 255
img = Image.fromarray(binary_transform, 'L')
img.save('image.png')

The other answers (all good!) use OpenCV or PIL. 其他答案(都很好!)使用OpenCV或PIL。 Here's how you could create the image using numpngw , a small library that I wrote to create PNG files from numpy arrays. 这是使用numpngw创建图像的方法,这是我编写的一个小库,用于从numpy数组创建PNG文件。

First, here's my data for the example: 首先,这是示例的数据:

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, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 2, 2, 2, 2, 2, 0],
 [0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 2, 2, 2, 2, 2, 0],
 [0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 2, 2, 2, 2, 2, 0],
 [0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 2, 2, 2, 2, 2, 0],
 [0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 2, 2, 2, 2, 2, 0],
 [0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 2, 2, 2, 2, 2, 0],
 [0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 2, 2, 2, 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, 0]]

Create the image using numpngw.write_png() : 使用numpngw.write_png()创建图像:

In [174]: import numpy as np

In [175]: import numpngw

In [176]: numpngw.write_png("foo.png", (np.array(x) > 0).astype(np.uint8), bitdepth=1)

Here's the image: 这是图片: 图片

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

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