I have a set of 640x480 images. I’m converting these images to a binary file format via Matlab… Now, I need to load each of the binary files into a buffer in Python, and then read data from that buffer. Can anyone help me out with how I might do that, or suggest a link? –Thanks.
Asked
Active
Viewed 944 times
1
-
Are you saving it to a .mat file? – Cody Piersall Aug 19 '15 at 20:16
-
1have a look at io.BytesIO https://docs.python.org/2/library/io.html#io.BytesIO – Padraic Cunningham Aug 19 '15 at 20:21
-
1http://code.runnable.com/UqJdRnCIohYmAAGP/reading-binary-files-in-python-for-io – dsgdfg Aug 19 '15 at 20:38
-
Can't you just treat the file as your buffer and readline one line from the file at a time and do your processing that way? Just asking because "640x480" doesn't necessarily translate to a specific buffer size. You could also just read the whole file with .`open("image_file.jpg", mode="rb").read()` – Dan Aug 19 '15 at 20:47
-
1I have binary file. I want buffer my file because I gono send byte by byte with serial port and I think if I buffer the file, I can send bytes faster,am I right? – user3397145 Aug 19 '15 at 22:09
-
1I mean does reading **file** byte by byte slower than reading **buffer** byte by byte? – user3397145 Aug 19 '15 at 22:11
-
@user3397145 depends on the size of the file and how you are doing the reads. Reading with a Py_buffer generally means Python reads the file into memory before populating the buffer you receive – several things affect how that performs: Python 2 vs 3 (file I/O was overhauled in Python 3); the infamous GIL state, and other concurrency gotchas; the files’ disposition on disk and/or memory… etc. Reading with the unbuffered POSIX descriptor API is generally pretty fast (as is using a `std::istream_iterator
` in C++-land); `mmap()` can be very fast but not in all circumstances… basically. – fish2000 Jul 01 '16 at 02:47
1 Answers
0
If you are using the C-API, you are going to want to use a Py_buffer
structure (which is not a Python object – it is unrelated to PyObject*
-compatible structures in the C-API).
- Basic information is here: https://docs.python.org/2/c-api/buffer.html
If not using C, you will be interested in memoryview
– the Python-level analog to Py_buffer
– and the file I/O builtins and library module(s):

fish2000
- 4,289
- 2
- 37
- 76