Slight thread hijack, my apologies, and please move this elsewhere if there's a better place.
I have been working on raw video for modern EOS cams and it involved a lot of messing around trying to work out if a buffer was an image and if so what width and data format.
Attached is my pure python solution. Should be cross platform (only tested on Linux).
Automatically guesses image width before display. Attempts to decode from 24 bpp / 8 bit RGB, 16 bpp / 8 bit YUV and 42 bpp / 14 bit Bayer. Decoding is handled by OpenCV, which supports many formats, should you want to add more. All the maths stuff is done in numpy so it's fast.
The cool part is the automatic width guessing. It's a very simple algorithm that I stole from SO. Because of this, you just give it a buffer and it displays the correct image - most of the time, for me. Feel free to steal any and all ideas to add them to your code
EDIT: example usage (press any key to cycle through displayed image decoding type):
./display_buf.py image_buffers/14bit_bayer_02.bin