After you've read the file, data
is a bytes
object, which can behave like a list of numbers, but isn't one, while new
is an actual list of numbers. Binary files support writing only bytes, so that's what you need to give it.
One solution is to replace file.write(new)
with file.write(bytes(new))
.
And here is a shorter rewrite of the code:
with open(filename+'.bmp', 'rb') as in_file:
data = in_file.read()
new_data = bytes(byte//2*2 for byte in data)
with open(filename+'.bmp', 'wb') as out_file:
out_file.write(new_data)
Note that the BMP format contains some headers, not just pixel data, so it will probably become corrupt after such modification.