Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import struct
- def writeBMP(filename, matrix):
- height = len(matrix)
- if height < 1:
- raise Exception('Height must be >0')
- width = len(matrix[0])
- if width < 1:
- raise Exception('Width must be >0')
- data = sum(reversed(matrix), [])
- with open(filename, 'wb') as file:
- file.write(struct.pack('<H', 0x4D42)) # Signature.
- file.write(struct.pack('<I', width * height * 4 + 54)) # File size
- file.write(struct.pack('<III', 0, 0x36, 0x28)) # Offset and other tech data.
- file.write(struct.pack('<ii', width, height))
- file.write(struct.pack('<HH', 1, 0x20)) # Tech data and bitcount.
- file.write(struct.pack('<IIiiII', 0, 0, 0, 0, 0, 0,)) # Zeroes.
- for pixel in data:
- r, g, b = pixel
- file.write(struct.pack('<BBBB', b, g, r, 0xFF))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement