Created
October 25, 2020 19:55
-
-
Save acedrew/4c9a6fe301328acbfd814665b14f30cb to your computer and use it in GitHub Desktop.
Script to convert gif to 16bit pixel buffers for M5StickC Screen
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
from PIL import Image, ImageSequence | |
import numpy as np | |
import argparse | |
import os | |
def load_gif(filename: str): | |
return Image.open(filename) | |
def iterate_frames(img: Image): | |
return ImageSequence.Iterator(img) | |
def generate_output_array(frame: Image): | |
return np.array(frame.copy().convert('RGB').getdata(), dtype=np.uint8) | |
def gif_to_bin(filename: str): | |
img = Image.open(filename) | |
if not os.path.exists(filename[:-4]): | |
os.mkdir(filename[:-4]) | |
for i, frame in enumerate(ImageSequence.Iterator(img)): | |
frame_array = np.array(frame.resize((160,80)).copy().convert('RGB').getdata(), dtype=np.uint8) | |
output_data = np.array([(((el[0] >> 3) << 11) + ((el[1] >> 2) << 5) + (el[2] >>3)) for el in frame_array.reshape(-1, 3)], dtype=np.uint16) | |
output_data.tofile(f"{filename[:-4]}/{i:02x}.bin") | |
def main(): | |
parser = argparse.ArgumentParser(description="GIF to Bin Converter") | |
parser.add_argument('gif', type=str, help="GIF or WEBM file to convert") | |
args = parser.parse_args() | |
gif_to_bin(args.gif) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment