Skip to content

Instantly share code, notes, and snippets.

@antiface
Forked from mcrisc/bmp_negator.py
Created August 6, 2016 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antiface/02969d3d14e4ff80ea33424e4b81b7eb to your computer and use it in GitHub Desktop.
Save antiface/02969d3d14e4ff80ea33424e4b81b7eb 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