0

我正在尝试将 pypng 数据结构转换为 numpy 数组(使用 PIL,您可以调用 numpy.array(img) 并且它可以工作),但我不确定如何使用 pypng。我需要使用 48 位图像,所以我需要使用 pypng。

我已将文档中建议的方法改编为 python3,但它似乎给了我错误的类型。

有我的尝试:

>>> import png
>>> reader = png.Reader('encoded_0000000.png')
>>> pngdata = reader.read()
>>> import numpy
>>> nparr = numpy.asarray(map(np.uint16, pngdata[2]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'np' is not defined
>>> nparr = numpy.asarray(map(numpy.uint16, pngdata[2]))
>>> nparr.shape
()
>>> nparr
array(<map object at 0x0000015F758F8DD8>, dtype=object)
>>># ^ This seems to be an incorrect object type, not a 3D array 
>>> nparr = numpy.asarray(numpy.uint16, pngdata[2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Miniconda3\envs\base\lib\site-packages\numpy\core\numeric.py", line 492, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: data type not understood
>>>
>>> nparr = numpy.asarray(pngdata[2], type=numpy.uint16)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: asarray() got an unexpected keyword argument 'type'
>>> nparr = numpy.asarray(pngdata[2], dtype=numpy.uint16)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Miniconda3\envs\base\lib\site-packages\numpy\core\numeric.py", line 492, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'generator'
>>> nparr = numpy.asarray(pngdata[2], dtype=numpy.uint16)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Miniconda3\envs\base\lib\site-packages\numpy\core\numeric.py", line 492, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'generator'
>>> nparr = numpy.asarray(pngdata, dtype=numpy.uint16)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Miniconda3\envs\base\lib\site-packages\numpy\core\numeric.py", line 492, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'generator'
>>> pngdata
(512, 256, <generator object Reader.iter_bytes_to_values at 0x0000015F758DBAF0>, {'greyscale': False, 'planes': 3, 'bitdepth': 16, 'alpha': False, 'interlace': 0, 'size': (512, 256)})
4

0 回答 0