Skip to content

Instantly share code, notes, and snippets.

@mcrisc
Created June 14, 2010 17:26
Show Gist options
  • Save mcrisc/437981 to your computer and use it in GitHub Desktop.
Save mcrisc/437981 to your computer and use it in GitHub Desktop.
# coding: utf-8
# Python Bitmap (bmp) Image Negation (using module struct)
import struct
with open('in.bmp', 'rb') as fd, open('out.bmp', 'wb') as out:
s = fd.read(1)
count = 1
while s:
if count < 37: # header bytes
out.write(s)
else: # data bytes
b = struct.unpack('B', s)[0]
out.write(struct.pack('B', 255 - b))
s = fd.read(1)
count += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment