Python OpenCV convert image to byte string?

If you have an image img (which is a numpy array) you can convert it into string using: >>> img_str = cv2.imencode(‘.jpg’, img)[1].tostring() >>> type(img_str) ‘str’ Now you can easily store the image inside your database, and then recover it by using: >>> nparr = np.fromstring(STRING_FROM_DATABASE, np.uint8) >>> img = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR) where you need … Read more

How can I convert a binary file to the text declaring a C/C++ array with that content?

On Debian and other Linux distros is installed by default (along with vim) the xxd tool, which, given the -i option, can do what you want: matteo@teodeb:~/Desktop$ echo Hello World\! > temp matteo@teodeb:~/Desktop$ xxd -i temp unsigned char temp[] = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x0a }; … Read more

Why Doesn’t reinterpret_cast Force copy_n for Casts between Same-Sized Types?

why doesn’t reinterpret_cast handle that for me? One reason is that the size, alignment, and bit representations aren’t specified, so such a conversion wouldn’t be portable. However, that wouldn’t really justify making the behaviour undefined, just implementation-defined. By making it undefined, the compiler is allowed to assume that expressions of unrelated types don’t access the … Read more

Which C datatype can represent a 40-bit binary number?

If you’re using a C99 or C11 compliant compiler, then use int_least64_t for maximum compatibility. Or, if you want an unsigned type, uint_least64_t. These are both defined in <stdint.h> <stdint.h> usually also defines int64_t, but since it’s not required by the standard, it may not be defined in every implementation. However: int_least64_t – at least … Read more

Is there a tool to know whether a value has an exact binary representation as a floating point variable?

Here’s a Python snippet that does exactly what you ask for; it needs Python 2.7 or Python 3.x. (Earlier versions of Python are less careful with floating-point conversions.) import decimal, sys input = sys.argv[1] if decimal.Decimal(input) == float(input): print(“Exactly representable”) else: print(“Not exactly representable”) Usage: after saving the script under the name ‘’, mdickinson$ python … Read more