Fix fault in python ImageOutput.write_* functions

The buffer to be written was assumed to be of the same format as
specified in the ImageOutput's spec, which in generally is not true and
leads to a wrong calculation of the buffer size.

This commit makes the python write_* functions do something similar to
the C++ ImageOutput::write_image function to calculate the buffer size.

It works for me. I hope I didn't overlook anything.
No, I think it's fine as it is. I'm adding a test and then will merge it.