Skip to content

Instantly share code, notes, and snippets.

@bofh
Created November 22, 2022 09:55
Show Gist options
  • Save bofh/ece6f40b6393b031d034e0b4103c6e33 to your computer and use it in GitHub Desktop.
Save bofh/ece6f40b6393b031d034e0b4103c6e33 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from PIL import Image, ImageOps
from argparse import ArgumentParser
import sys
import math
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 825
if SCREEN_WIDTH % 2:
print("image width must be even!", file=sys.stderr)
sys.exit(1)
parser = ArgumentParser()
parser.add_argument('-i', action="store", dest="inputfile")
parser.add_argument('-o', action="store", dest="outputfile")
args = parser.parse_args()
im = Image.open(args.inputfile)
# convert to grayscale
im = im.convert(mode='L')
im.thumbnail((SCREEN_WIDTH, SCREEN_HEIGHT), Image.ANTIALIAS)
with open(args.outputfile, 'wb') as f:
for y in range(0, im.size[1]):
byte = 0
done = True
for x in range(0, im.size[0]):
l = im.getpixel((x, y))
if x % 2 == 0:
byte = l >> 4
done = False;
else:
byte |= l & 0xF0
f.write(byte.to_bytes(1, 'big'))
done = True
if not done:
f.write(byte.to_bytes(1, 'big'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment