Skip to content

Instantly share code, notes, and snippets.

@sidwarkd
Last active February 27, 2024 14:51
Show Gist options
  • Save sidwarkd/46b2bcb6b93aea173a7e175d8b045714 to your computer and use it in GitHub Desktop.
Save sidwarkd/46b2bcb6b93aea173a7e175d8b045714 to your computer and use it in GitHub Desktop.
A simple script to convert a bmp file to a header file for the GDEP015OC1 e-paper display
#!/usr/bin/env python
# You need to install Pillow for this script to work
# pip install Pillow
import argparse
from PIL import Image
# Currently this utility script is very specific to my needs for generating
# a header file from a 200x200 pixel bmp. It could easily be expanded to work
# for different sizes
def generateHeaderFromBitmap(bitmap_path, header_path, name=None):
headerRows = []
with Image.open(bitmap_path) as original:
all_pixels = list(original.convert('1', None, Image.NONE).getdata())
# Loop through every scan line
for rows in xrange(0, 200):
rowstart = rows*200
rowEnd = rowstart + 200
rowPixels = all_pixels[rowstart:rowEnd]
rowBytes = [0xFF] * 25
# For a 200px wide black/white row each scan line can be represented as 25 bytes
for byte_index in range(0,25):
byteStart = byte_index * 8
byteEnd = byteStart + 8
bytePixels = rowPixels[byteStart:byteEnd]
# Translate each pixel value into the correct scan line byte
for pixel_index, pixel_value in enumerate(bytePixels):
if pixel_value:
# pixel not set
rowBytes[byte_index] |= (1 << (7 - pixel_index % 8))
else:
# pixel is set
rowBytes[byte_index] &= 0xFF ^ (1 << (7 - pixel_index % 8))
headerRow = [format(val, '#04x') for val in rowBytes]
headerRows.append(' {},\n'.format(','.join(headerRow)))
with open(header_path, 'w') as headerFile:
headerFile.write('const unsigned char {}[5000] PROGMEM = {{\n'.format(name or 'bmp_data'))
headerFile.writelines(headerRows)
headerFile.write('};')
parser = argparse.ArgumentParser(description='Create a header file from a Bitmap image.')
parser.add_argument('bmp', nargs=1, help='the bitmap file')
parser.add_argument('header', nargs=1, help='the output header file path')
parser.add_argument('-n', metavar='obj_name', nargs=1, help='name of the header object')
args = parser.parse_args()
generateHeaderFromBitmap(args.bmp[0], args.header[0], args.n[0] if args.n else None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment