Skip to content

Instantly share code, notes, and snippets.

@graeme-winter
Created June 11, 2022 05:07
Show Gist options
  • Save graeme-winter/70fced0c9ea1f19c1fcbc95c3eae77db to your computer and use it in GitHub Desktop.
Save graeme-winter/70fced0c9ea1f19c1fcbc95c3eae77db to your computer and use it in GitHub Desktop.
Python script to make movie from stack of jpeg images
import sys
import os
import numpy as np
from PIL import Image, ImageFont, ImageDraw
import cv2
def main(file_template, start=0, end=100000):
font = ImageFont.truetype("FreeMono.ttf", 32)
capture = cv2.VideoCapture(0)
out = cv2.VideoWriter(
"out.mp4", cv2.VideoWriter_fourcc(*"mp4v"), 10.0, (1640, 820)
)
print(f"Processing images {start} to {end} inclusive")
for j in range(start, end + 1):
filename = file_template % j
print(filename)
if not os.path.exists(filename):
continue
image = Image.open(filename)
timestamp = image._getexif()[36867]
day, time = timestamp.split()
day = day.replace(":", "/")
time = "%s %05d" % (time[:5], j)
draw = ImageDraw.Draw(image)
draw.text((16, 16), f"{time}", (0xFF, 0xFF, 0xFF), font=font)
# this inverts RGB but we don't care because monochrome
out.write(np.asarray(image))
out.release()
capture.release()
if __name__ == "__main__":
main(sys.argv[1], start=int(sys.argv[2]), end=int(sys.argv[3]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment