Numpy 使用(可能使用scipy.misc.imread)读回图像数据

Numpy 使用(可能使用scipy.misc.imread)读回图像数据,numpy,scipy,python-imaging-library,Numpy,Scipy,Python Imaging Library,这个问题可能与以图像形式存储和检索numpy数组有关。因此,我将二进制值数组保存到图像中(使用scipy.misc.toimage功能): 请注意,我使用模式1(1位像素,黑白,每字节存储一个像素)保存数据。现在,当我试着读回它时,就像: data = scipy.misc.imread("arrayimage.png") 生成的数据数组返回为全零 问题是:有没有其他方法从图像中检索数据,并严格要求图像应以模式1创建。谢谢。我想你想要这个: from PIL import Image impo

这个问题可能与以图像形式存储和检索numpy数组有关。因此,我将二进制值数组保存到图像中(使用
scipy.misc.toimage
功能):

请注意,我使用模式
1
(1位像素,黑白,每字节存储一个像素)保存数据。现在,当我试着读回它时,就像:

data = scipy.misc.imread("arrayimage.png")
生成的
数据
数组返回为全零

问题是:有没有其他方法从图像中检索数据,并严格要求图像应以模式
1
创建。谢谢。

我想你想要这个:

from PIL import Image
import numpy

# Generate boolean data
data=numpy.random.randint(0, 2, size=(100, 1),dtype="bool")

# Convert to PIL image and save as PNG
Image.fromarray(data).convert("1").save("arrayimage.png")
检查使用ImageMagick获得的信息

identify -verbose arrayimage.png
样本输出

Image: arrayimage.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 1x100+0+0
  Units: Undefined
  Colorspace: Gray
  Type: Bilevel                                    <--- Bilevel means boolean
  Base type: Undefined
  Endianess: Undefined
  Depth: 8/1-bit
  Channel depth:
    Gray: 1-bit
  Channel statistics:
    Pixels: 100
    Gray:
      min: 0  (0)
      max: 255 (1)
      mean: 130.05 (0.51)
      standard deviation: 128.117 (0.502418)
      kurtosis: -2.01833
      skewness: -0.0394094
      entropy: 0.999711
  Colors: 2
  Histogram:
        49: (  0,  0,  0) #000000 gray(0)          <--- half the pixels are black
        51: (255,255,255) #FFFFFF gray(255)        <--- half are white
  Colormap entries: 2
  Colormap:
         0: (  0,  0,  0,255) #000000FF graya(0,1)
         1: (255,255,255,255) #FFFFFFFF graya(255,1)
Image:arrayimage.png
格式:PNG(便携式网络图形)
Mime类型:图像/png
类别:伪类别
几何图形:1x100+0+0
单位:未定义
颜色空间:灰色

类型:二层供参考:您可以替换
numpy.array([random.randint(0,1)表示范围(100)]。使用
numpy.random.randint(0,2,size=(100,1))
重塑(100,1)
。您说的“模式1”是什么意思?PNG图像本身没有模式,它们有颜色选项和颜色类型。也许您需要将0和1的值缩放为0和255。我的答案解决了您的问题吗?如果是这样,请考虑接受它作为您的答案-点击空心蜱/支票旁边的选票计数。如果没有,请说出什么不起作用,以便我或其他人可以进一步帮助您。谢谢
Image: arrayimage.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 1x100+0+0
  Units: Undefined
  Colorspace: Gray
  Type: Bilevel                                    <--- Bilevel means boolean
  Base type: Undefined
  Endianess: Undefined
  Depth: 8/1-bit
  Channel depth:
    Gray: 1-bit
  Channel statistics:
    Pixels: 100
    Gray:
      min: 0  (0)
      max: 255 (1)
      mean: 130.05 (0.51)
      standard deviation: 128.117 (0.502418)
      kurtosis: -2.01833
      skewness: -0.0394094
      entropy: 0.999711
  Colors: 2
  Histogram:
        49: (  0,  0,  0) #000000 gray(0)          <--- half the pixels are black
        51: (255,255,255) #FFFFFF gray(255)        <--- half are white
  Colormap entries: 2
  Colormap:
         0: (  0,  0,  0,255) #000000FF graya(0,1)
         1: (255,255,255,255) #FFFFFFFF graya(255,1)