Skip to content

Instantly share code, notes, and snippets.

@larshb
Last active October 12, 2018 14:15
Show Gist options
  • Save larshb/268d71d72d43fbd068e30333271da9f5 to your computer and use it in GitHub Desktop.
Save larshb/268d71d72d43fbd068e30333271da9f5 to your computer and use it in GitHub Desktop.
A simple python script to convert a raw binary coded image into a bitmap file. (applying metadata to a header)
import struct
# Filenames
IFILE = "raw.bsq"
OFILE = IFILE + ".bmp"
# Image dimensions
NX = 512
NY = 2000
# Bitmap header
BMP_ID = "BM"
SIZE_HDR = 14
SIZE_DIB = 40
CHANNELS = 1
PLANES = 1
BPC = 16 # Bits per component
BPP = CHANNELS*BPC # Bits per pixel
OFFSET = SIZE_HDR+SIZE_DIB
SIZE_IMG = NX*NY*BPP
SIZE_FIL = OFFSET+SIZE_IMG
head = BMP_ID + struct.pack('IHHIIIIHHIIIIII',\
SIZE_FIL,0,0,OFFSET,SIZE_DIB,NX,NY,PLANES,BPP,0,SIZE_IMG,0,0,0,0)
data = open(IFILE,"rb").read();
open(OFILE,"wb").write(head + data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment