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

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 ‘exactly_representable.py’, mdickinson$ python … Read more