Skip to content

Instantly share code, notes, and snippets.

@KTibow
Created March 25, 2023 02:01
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 KTibow/07b1f6a60b3a9bb8b657614aafd96c61 to your computer and use it in GitHub Desktop.
Save KTibow/07b1f6a60b3a9bb8b657614aafd96c61 to your computer and use it in GitHub Desktop.
from PIL import Image, ImageOps
image = Image.open("snapshot.jpg")
width, height = image.size
if width > 178 or height > 128:
image.thumbnail((178, 128))
image = ImageOps.pad(image, (178, 128), color=(255, 255, 255))
image = image.convert("1")
width, height = 178, 128
binary_data = bytes([width, height])
for y in range(height):
cur_byte = 0
cur_bit = 0
for x in range(width):
if image.getpixel((x, y)) == 0:
cur_byte += pow(2, cur_bit)
cur_bit += 1
if cur_bit == 8:
binary_data += bytes([cur_byte])
cur_byte = 0
cur_bit = 0
binary_data += bytes([cur_byte])
with open("out.rgf", "wb") as out:
out.write(binary_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment