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

packing and unpacking variable length array/string using the struct module in python

The struct module does only support fixed-length structures. For variable-length strings, your options are either: Dynamically construct your format string (a str will have to be converted to a bytes before passing it to pack()): s = bytes(s, ‘utf-8’) # Or other appropriate encoding struct.pack(“I%ds” % (len(s),), len(s), s) Skip struct and just use normal … Read more

Jump to byte address in vim?

Yes, there is. It’s the normal mode command go. From :h go: [count]go         Go to {count} byte in the buffer. For example, 42go jumps to byte 42. In order to jump to a hexadecimal or octal address you would need to construct a command with :normal! go or the equivalent :goto, and the str2nr() … Read more

What are important points when designing a (binary) file format? [closed]

Take a look at the PNG spec. This format has some very good rationale behind it. Also, decide what’s important for your future format: compactness, compatibility, allowing to embed other formats (different compression algorithms) inside it. Another interesting example would be the Google’s protocol buffers, where size of the transferred data is the king. As … Read more

Binary String to Integer

You could use a Regex to check that it is “^[01]+$” (or better, “^[01]{1,32}$”), and then use Convert? of course, exceptions are unlikely to be a huge problem anyway! Inelegant? maybe. But they work. Example (formatted for vertical space): static readonly Regex binary = new Regex(“^[01]{1,32}$”, RegexOptions.Compiled); static void Main() { Test(“”); Test(“01101”); Test(“123”); Test(“0110101101010110101010101010001010100011010100101010”); … Read more

Convert A String (like testing123) To Binary In Java [closed]

The usual way is to use String#getBytes() to get the underlying bytes and then present those bytes in some other form (hex, binary whatever). Note that getBytes() uses the default charset, so if you want the string converted to some specific character encoding, you should use getBytes(String encoding) instead, but many times (esp when dealing … Read more