convert pypng to numpy array - python-3.x

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)})

Related

declaring a python variable in a list [data] = self.read()?

while studying the open source repo of Odoo I found a line of code that I don't understand like the following
[data] = self.read()
found there https://github.com/odoo/odoo/blob/8f297c9d5f6d31370797d64fee5ca9d779f14b81/addons/hr_holidays/wizard/hr_holidays_summary_department.py#L25
I really would like to know why would you put the variable in a list
It seems to ensure that [data] is an iterable of one item and therefore unpacks the first value from self.read()
It cannot be assigned to a non-iterable
>>> [data] = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot unpack non-iterable int object
Works for iterable types, though must have a length equal to one
>>> [data] = {'some':2}
>>> data
'some'
>>> [data] = {'foo':2, 'bar':3}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 1)
>>> [data] = [1]
>>> data
1
>>> [data] = [[1]]
>>> data
[1]
>>> [data] = [1, 2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 1)
>>> [data] = []
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 1, got 0)

Pickle TypeError: a bytes-like object is required, not 'str'

Calling getVocab() gives me a TypeError: a bytes-like object is required, not 'str'
def getVocab():
file = open("../words/vocab")
return pickle.load(file)
The vocab file looks like so
(dp0
S'slope'
p1
I516
sS'pointing'
p2
I3
sS'hats'
p3
I4
sS'people'
p4
I825
sS'yellow'
p5
I5
The traceback is given below
2020-05-05 21:46:52.986615: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1187]
Traceback (most recent call last):
File "main.py", line 181, in <module>
m = Main()
File "main.py", line 22, in __init__
self.texti = loadData.TextIterator(self.batchSize, self.seqLen)
File "/home/ichimichi/Downloads/data-of-multimodal-sarcasm-detection-e428b1497dea29d7e39592ea1d266c1de44084f1/codes/loadData.py", line 25, in __init__
self.word2id = self.getVocab()
File "/home/ichimichi/Downloads/data-of-multimodal-sarcasm-detection-e428b1497dea29d7e39592ea1d266c1de44084f1/codes/loadData.py", line 48, in getVocab
return pickle.load(file)
TypeError: a bytes-like object is required, not 'str'

How to get a useful exception message from decimal in python 3?

With Python 2, creating a Decimal with an invalid string produces a useful error message:
>>> import decimal
>>> decimal.Decimal('spam')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 547, in __new__
"Invalid literal for Decimal: %r" % value)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 3872, in _raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: 'spam'
While Python 3 produces a not-so-helpful message:
>>> import decimal
>>> decimal.Decimal('spam')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
Is there any way to get a useful message like "Invalid literal for Decimal: 'spam'" from the exception in Python 3?
I'm using Python 2.7.15 and Python 3.7.2, both on darwin.
Addenda:
It looks like Python 2 once had a not-very-helpful message for decimal.InvalidOperation: https://bugs.python.org/issue1770009
This situation looks analogous but most of it goes over my head: https://bugs.python.org/issue21227
You could monkey-patch the decimal module.
import decimal
def safe_decimal(something):
try:
funct_holder(something)
except Exception as e:
new_errror = Exception("Hey silly that's not a decimal, what should I do with this? {}".format(something))
raise new_errror from None
funct_holder = decimal.Decimal
decimal.Decimal = safe_decimal
Then you could use the monkey patched version as so
>>> decimal.Decimal('hello')
Traceback (most recent call last):
File "<input>", line 12, in <module>
File "<input>", line 6, in safe_decimal
Exception: Hey silly that's not a decimal, what should I do with this? hello

AttributeError: module 'readline' has no attribute 'set_completer_delims'

>>> import pdb
>>> x = [1,2,3,4,5]
>>> y = 6
>>> z = 7
>>> r1 = y+z
>>> r1
13
>>> r2 = x+y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "int") to list
>>> pdb.set_trace()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/pdb.py", line 1585, in set_trace
Pdb().set_trace(sys._getframe().f_back)
File "/usr/lib/python3.6/pdb.py", line 156, in __init__
readline.set_completer_delims(' \t\n`##$%^&*()=+[{]}\\|;:\'",<>?')
AttributeError: module 'readline' has no attribute 'set_completer_delims'
>>>
Whats problem? run python3.6 an error occurred
I just try to pdb on Cygwin.
(Note that other lib is okay)
In my case the problem was fixed by installing pyreadline:
pip install pyreadline
Please try it.
More info: https://github.com/winpython/winpython/issues/544

dtypes returning AttributeError

I'm using pandas for the first time. I have read data from an Excel file into a data frame, but can't seem to do anything with it.
The dataframe is called ParsedData. It exists and contains data:
>>> ParsedData
>>> USD# County Name USD Name Density Area
>>> 0 D0101 Neosho Erie 0.847692 325.0
>>> ...
I can confirm that it is a dataframe:
>>> type(ParsedData)
>>> <class 'pandas.core.frame.DataFrame'>
However, attempting to determine the dtypes of my columns fails:
>>> ParsedData.dtypes
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
ParsedData.dtypes
File "C:\Python34\lib\idlelib\rpc.py", line 611, in displayhook
text = repr(value)
File "C:\Python34\lib\site-packages\pandas\core\base.py", line 72, in __repr__
return str(self)
File "C:\Python34\lib\site-packages\pandas\core\base.py", line 51, in __str__
return self.__unicode__()
File "C:\Python34\lib\site-packages\pandas\core\series.py", line 982, in __unicode__
width, height = get_terminal_size()
File "C:\Python34\lib\site-packages\pandas\io\formats\terminal.py", line 33, in get_terminal_size
return shutil.get_terminal_size()
File "C:\Python34\lib\shutil.py", line 1071, in get_terminal_size
size = os.get_terminal_size(sys.__stdout__.fileno())
AttributeError: 'NoneType' object has no attribute 'fileno'
What's going on here?

Resources