binary

Python read binary file of unsigned 16 bit integers

Python read binary file of unsigned 16 bit integers Question: I must read a binary file in Python, and store its content in an array. The information I have on this file is that filename.bin is of size 560×576 (height x width) with 16 b/p, i.e., unsigned 16-bit integer for each pixel This is what …

Total answers: 1

How to strip off left side of binary number in Python?

How to strip off left side of binary number in Python? Question: I got this binary number 101111111111000 I need to strip off the 8 most significant bits and have 11111000 at the end. I tried to make 101111111111000 << 8, but this results in 10111111111100000000000, it hasn’t the same effect as >> which strips …

Total answers: 4

B&W image to binary array

B&W image to binary array Question: I want to convert my b&w image(.png) to binary array(black is 1 white is 0). I have written some code, but it’s not working. Error says: argument 2 to map() must support iteration. Here is my code: from PIL import Image from resizeimage import resizeimage import sys def threshold(col): …

Total answers: 2

Getting the position of 1-bits in a python Long object

Getting the position of 1-bits in a python Long object Question: Let’s say I have a very very large python integer, in python 2.7 (though if I need to, I don’t mind switching to python 3). Something bigger than say, 2^100000. What is the fastest way by which I can find the positions of all …

Total answers: 3

Python: How can i invert string "0110010"?

Python: How can i invert string "0110010"? Question: How to convert strings containing only 0s and 1s in most efficient and elegant way? Ex: input: "0110010" output: "1001101" Thanks in advance. Asked By: Avo Asatryan || Source Answers: This piece of code works with strings, try it: >>> from string import maketrans >>> >>> initial_string …

Total answers: 4

How to return binary data from lambda function in AWS in Python?

How to return binary data from lambda function in AWS in Python? Question: I cannot get python lambda to return binary data. The node-template for thumbnail images works fine but I cannot get a python lambda to work. Below is the relevant lines from my lambda. The print(“image_data ” + image_64_encode) line prints a base64 …

Total answers: 5

How to decode jpg image from memory?

How to decode jpg image from memory? Question: I can read a jpg image from disk via PIL, Python OpenCV, etc. into a numpy array via some built-in functions such as (in the case of OpenCV) arr= cv2.imread(filename). But how do I decode a jpg in binary format directly from memory? Use case: I want …

Total answers: 3

Python converting from base64 to binary

Python converting from base64 to binary Question: I have a problem about converting a base64 encoded string into binary. I am collecting the Fingerprint2D in the following link, url = “https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/108770/property/Fingerprint2D/xml” Fingerprint2D=AAADccB6OAAAAAAAAAAAAAAAAAAAAAAAAAA8WIEAAAAAAACxAAAAHgAACAAADAzBmAQwzoMABgCI AiTSSACCCAAhIAAAiAEMTMgMJibMsZuGeijn4BnI+YeQ0OMOKAACAgAKAABQAAQEABQAAAAAAAAA AA== The descriptiong in the Pubchem says that this is 115 byte string, and it should be 920 bits when converted into …

Total answers: 2

Python: How can I print bytes?

Python: How can I print bytes? Question: I’m using Python 2.7. I want to print a binary in decimal, but I’m receiving an error, which I do not understand. Eg. I am trying: print 0b111 I am expecting 7. But it returns: Unescaped left brace in regex is deprecated, passed through in regex; marked by …

Total answers: 1

Parsing Yann LeCun's MNIST IDX file format

Parsing Yann LeCun's MNIST IDX file format Question: I would like to understand how to open this version of the MNIST data set. For example, the training set label file train-labels-idx1-ubyte is defined as: TRAINING SET LABEL FILE (train-labels-idx1-ubyte): [offset] [type] [value] [description] 0000 32 bit integer 0x00000801(2049) magic number (MSB first) 0004 32 bit …

Total answers: 4