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.