Last active
October 12, 2018 14:15
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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