Skip to content

Instantly share code, notes, and snippets.

@pkamp3
Last active May 24, 2017 23:40
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 pkamp3/5ea656b8b67c9d8f19daa72ff26bcf89 to your computer and use it in GitHub Desktop.
Save pkamp3/5ea656b8b67c9d8f19daa72ff26bcf89 to your computer and use it in GitHub Desktop.
Animated gif and movie converter for Hackpackv3
"""
Convert animated gifs to uint_32t encoded colors for use on the Hackpackv3.
Got a Hackpackv3? Here's an easy way to convert animated gifs or
movies into encoded 4x4 images; you can directly use this in the .ino
file to overwrite the 'custom_animation_lut'
Sample use (bash example, change for your platform):
# First, dump all gif or movie frames to pngs
# (If you don't have ffmpeg you'll have to install it)
export FILENAME=somemovie.mp4
#export FILENAME=funnygif.gif
ffmpeg -i $FILENAME pic%03d.png
# Use the script to convert to a structure ready for your Hackpackv3
python animated_gif_converter.py -o output.h
# Optional - use -d to dump preview images (to bmp)
# python animated_gif_converter.py -o output.h -d
# Optional - use -f to change filetype
# python animated_gif_converter.py -o output.h -f bmp
LICENSE: MIT
"""
from __future__ import print_function
from PIL import Image
from optparse import OptionParser
import glob
import sys
# Globals & Constants - Screen size, verbose debug
HP_SIZE = 4, 4
verbose = False
def main(argv=None):
"""
Parse command line options and dump to a header file.
Only -o is required, it is where we will place the converted images.
"""
parser = OptionParser()
parser.add_option('-o', action="store", dest="outputfile")
parser.add_option('-d', action="store", dest="dumpfiles")
parser.add_option('-f', action="store", dest="filetype")
parser.add_option('-v', action="store_true", dest="verbose")
(opts, args) = parser.parse_args()
if opts.outputfile is not None:
outputfile = opts.outputfile
else:
print ("Use the -o option and pass an outputfile")
return 1
filetype = opts.filetype if opts.filetype else "png"
global verbose
verbose = True if opts.verbose else False
with open(outputfile, 'w+') as f:
firstimg = True
lutstr = "uint32_t custom_animation_lut[][16] = { "
num = 0
for file in glob.glob("*." + filetype):
print_v(file)
im = Image.open(file)
im = im.convert('RGB')
width, height = im.size
# Crop to the minimum dimension x or y
dim = min([width, height])
left = (width - dim) / 2
top = (height - dim) / 2
right = (width + dim) / 2
bottom = (height + dim) / 2
im = im.crop((left, top, right, bottom))
# Rescale to the size of the display
im.thumbnail(HP_SIZE)
width, height = im.size
# Build a uint32_t struct
imstr = ""
if not firstimg:
imstr += ","
else:
firstimg = False
imstr += "\n { "
firstone = True
for i in range(height):
for j in range(width):
r, g, b = im.getpixel((j, i))
print_v(j, i, ": ", r, g, b)
pixstr = ""
if not firstone:
pixstr += ","
else:
firstone = False
pixstr += "0x"
pixstr += hex(r)[2:].zfill(2).upper()
pixstr += hex(g)[2:].zfill(2).upper()
pixstr += hex(b)[2:].zfill(2).upper()
imstr += pixstr
imstr += " } "
lutstr += imstr
if opts.dumpfiles is not None:
im.save(str(num) + ".bmp")
num += 1
lutstr += "\n};"
f.write(lutstr)
print_v(lutstr)
def print_v(*args):
"""Print debug information only if verbose."""
global verbose
if verbose:
for arg in args:
print(arg, end=' ')
print()
if __name__ == "__main__":
sys.exit(main())
export FILENAME=loading.gif
ffmpeg -i $FILENAME pic%03d.png
# Use the script to convert to a structure ready for your Hackpackv3
python animated_gif_converter.py -o output.h
# Optional - use -d to dump preview images (to bmp)
# python animated_gif_converter.py -o output.h -d
# Optional - use -f to change filetype
# python animated_gif_converter.py -o output.h -f bmp
# Paste the custom_animation_lut contents over the .ino's custom_animation_lut.
# Be sure to also edit the custom_animation_framerate for desired animated speed.
@pkamp3
Copy link
Author

pkamp3 commented May 24, 2017

loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment