0

I'm trying to convert pypng data struct to a numpy array (with PIL, you can just call numpy.array(img) and it works), but I'm not sure how to do it with pypng. I need to work with 48 bit images, so I need to use pypng.

I've adapted the method suggested in the docs to python3, but it seems to give me the wrong type.

There are my attempts:

>>> 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)})
dev_nut
  • 2,476
  • 4
  • 29
  • 49
  • Take a step back for a moment please. You want to read a 48-bit PNG, correct? And you feel you must use PyPNG because? – Mark Setchell Feb 28 '19 at 20:04
  • Because, I tried PIL and it reads it as a 3 channel, 8 bit image. PyPNG looked promising. But, I think opencv handles this correctly. It seems to load the image as 3 channel, 16 bit image. – dev_nut Feb 28 '19 at 20:13

0 Answers0