Skip to content

Instantly share code, notes, and snippets.

@KTibow
Created March 25, 2023 02:01
Show Gist options
  • Select an option

  • Save KTibow/07b1f6a60b3a9bb8b657614aafd96c61 to your computer and use it in GitHub Desktop.

Select an option

Save KTibow/07b1f6a60b3a9bb8b657614aafd96c61 to your computer and use it in GitHub Desktop.

Revisions

  1. KTibow created this gist Mar 25, 2023.
    28 changes: 28 additions & 0 deletions png_to_rgf.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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)