Last active
January 25, 2021 03:05
-
-
Save jaames/0ed9b1fb75549e508261eaea9e21c753 to your computer and use it in GitHub Desktop.
custom flipnote thumbnail tool
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
# Custom Flipnote (.ppm) Thumbnail Tool | |
# Create a ppm thumbnail from any 64 x 48 image | |
# Requires Pillow - https://github.com/python-pillow/Pillow | |
# | |
# By Jaames <github.com/jaames | @rakujira on twitter> | |
# | |
# Usage: python3 thumbtool.py input.ppm thumb.png | |
from PIL import Image | |
from sys import argv | |
# thumbnail palette | |
palette = [ | |
0xFF, 0xFF, 0xFF, | |
0x52, 0x52, 0x52, | |
0xFF, 0xFF, 0xFF, | |
0x9C, 0x9C, 0x9C, | |
0xFF, 0x48, 0x44, | |
0xC8, 0x51, 0x4F, | |
0xFF, 0xAD, 0xAC, | |
0x00, 0xFF, 0x00, | |
0x48, 0x40, 0xFF, | |
0x51, 0x4F, 0xB8, | |
0xAD, 0xAB, 0xFF, | |
0x00, 0xFF, 0x00, | |
0xB6, 0x57, 0xB7, | |
0x00, 0xFF, 0x00, | |
0x00, 0xFF, 0x00, | |
0x00, 0xFF, 0x00, | |
] | |
# create lookup image for the palette | |
palimg = Image.new("P", (16, 16)) | |
palimg.putpalette(palette * 16) | |
# open thumb image | |
img = Image.open(argv[2]) | |
img = img.convert("RGB") | |
# apply palette | |
# https://stackoverflow.com/questions/29433243/convert-image-to-specific-palette-using-pil-without-dithering/29438149 | |
img.load() | |
palimg.load() | |
im = img.im.convert("P", 0, palimg.im) | |
img = img._new(im) | |
# open donor ppm | |
with open(argv[1], "rb+") as ppm: | |
# seek to the start of the thumbnail data | |
ppm.seek(160) | |
# convert image into 8*8 tiled 4-bit bitmap and write to ppm | |
for tileIndex in range(0, 48): | |
tileX = tileIndex % 8 * 8 | |
tileY = tileIndex // 8 * 8 | |
for line in range(0, 8): | |
for pixel in range(0, 8, 2): | |
x = tileX + pixel | |
y = tileY + line | |
# each byte is comprised of 2 pixels | |
pix1 = img.getpixel((x, y)) % 16 | |
pix2 = img.getpixel((x + 1, y)) % 16 | |
ppm.write(bytes([pix1 | (pix2 << 4)])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment