Skip to content

Instantly share code, notes, and snippets.

@allyourcode
Created July 26, 2018 11:06
Show Gist options
  • Select an option

  • Save allyourcode/3c9a7d4acd29182982d64070f2d8758a to your computer and use it in GitHub Desktop.

Select an option

Save allyourcode/3c9a7d4acd29182982d64070f2d8758a to your computer and use it in GitHub Desktop.
Tools forbreaking a video down into a series of still images.
from __future__ import print_function
import csv
import os
import sys
import cv2
def freak_out(msg):
print('Failed:', msg, file=sys.stderr)
sys.exit(1)
def divide_and_round_up(dividend, divisor):
q, r = divmod(dividend, divisor)
if r:
q += 1
return q
def main():
PANEL_WIDTH = 16
PANEL_HEIGHT = 16
COLOR_COMPONENT_COUNT = 3
# Inspect input.
source, = sys.argv[1:]
if not os.path.exists(source):
freak_out('File not found at %s' % source)
# Make output directory. Name it after input.
destination = os.path.splitext(source)[0]
if os.path.exists(destination):
freak_out('Destination already exists.')
os.mkdir(destination)
# Load file.
content = cv2.imread(source)
if content is None:
freak_out('Failed to load %s' % source)
row_count, column_count, _ = content.shape
# TODO(danielwong): Freak out if these are not divisible by
# PANEL_HEIGHT and PANEL_WIDTH respectively.
# Iterate over panels.
for i in xrange(0, row_count, PANEL_HEIGHT):
for j in xrange(0, column_count, PANEL_WIDTH):
slice_ = content[
i:i+PANEL_HEIGHT, j:j+PANEL_WIDTH]
w, h, _ = slice_.shape
panel_content = slice_.reshape(
w * h, COLOR_COMPONENT_COUNT)
panel_file_name = '%d_%d.csv' % (
i / PANEL_HEIGHT, j / PANEL_WIDTH)
with open(os.path.join(destination, panel_file_name),
'wb') as panel_file:
writer = csv.writer(panel_file)
for color in panel_content:
writer.writerow(map(str, color))
print('Success!', file=sys.stderr)
if __name__ == '__main__':
main()
from __future__ import print_function
import csv
import os
import sys
import cv2
import numpy
# TODO(danielwong): These constants are used elsewhere
# (e.g. panelize). DRY! On the other hand, only depending on the std
# lib is ncie...
PANEL_WIDTH = 16
PANEL_HEIGHT = 16
COLOR_COMPONENT_COUNT = 3
def freak_out(msg):
print('Failed:', msg, file=sys.stderr)
sys.exit(1)
def main():
source, = sys.argv[1:]
# Make sure destination doesn't already exist.
destination = os.path.splitext(source)[0] + '.jpg'
if os.path.exists(destination):
freak_out('Destination already exists.')
out = numpy.zeros(
(PANEL_HEIGHT, PANEL_WIDTH, COLOR_COMPONENT_COUNT), int)
with open(source, 'rb') as panel_file:
reader = csv.reader(panel_file)
for k, color in enumerate(reader):
i, j = divmod(k, PANEL_WIDTH)
out[i,j] = map(int, color)
ok = cv2.imwrite(destination, out)
if __name__ == '__main__':
main()
from __future__ import print_function
import os
import sys
import cv2
def freak_out(msg):
print('Failed:', msg, file=sys.stderr)
sys.exit(1)
def main():
# Inspect input.
source, = sys.argv[1:]
if not os.path.exists(source):
freak_out('Source does not exist.')
# Make output directory. Name it after input.
destination = os.path.splitext(source)[0]
if os.path.exists(destination):
freak_out('Destination already exists.')
os.mkdir(destination)
# Open input.
capture = cv2.VideoCapture(source)
frame_number = 0
# Read each frame, and write it to its own file.
while True:
ok, content = capture.read()
if not ok:
freak_out('Unable to read frame %d (0 is first frame).'
% frame_number)
ok = cv2.imwrite(os.path.join(destination,
'%d.jpg' % frame_number),
content)
if not ok:
freak_out('Unable to write frame %d (0 is first frame).'
% frame_number)
frame_number += 1
# Clean up.
cap.release()
print('Success!', file=sys.stderr)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment