Skip to content

Instantly share code, notes, and snippets.

@OnlyInAmerica
Created December 9, 2014 22:28
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 OnlyInAmerica/c1666d284ce0ed0c5420 to your computer and use it in GitHub Desktop.
Save OnlyInAmerica/c1666d284ce0ed0c5420 to your computer and use it in GitHub Desktop.
Assemble an image hidden among multiple video frames.
'''
Construct a single image by stacking the bottom READ_HORIZ_PX
of each source frame of form image-XXXX.jpeg.
To create source frames of form image-XXXX.jpeg from a video file:
$ ffmpeg -i inputfile.avi image-%4d.jpeg
This was helpful in extracting a QR code partially displayed
across many frames.
'''
import os
from PIL import Image
READ_HORIZ_PX = 2 # Number of horizontal lines to read from each source frame
FRAME_SKIP_INTERVAL = -1 # -1 = use all frames. 2 = skip every other frame, 3 = skip every third frame etc
# Source frame dimens
FRAME_WIDTH = 320
FRAME_HEIGHT = 240
CROP_BOX_BOTTOM_MARGIN = 0 # Turned out not to help
# Crop box for source frame
# left, top, left+width, top+height
CROP_BOX = (0, FRAME_HEIGHT - READ_HORIZ_PX, FRAME_WIDTH, FRAME_HEIGHT - CROP_BOX_BOTTOM_MARGIN)
# Pasting box for destination frame. Will be adjusted after each paste
# left, top, left+width, top+height
paste_box = (0, 0, FRAME_WIDTH, READ_HORIZ_PX - CROP_BOX_BOTTOM_MARGIN)
dst_filename = 'output'
dst_img = None
def increment_paste_box(src):
new_src = (src[0], src[1] + READ_HORIZ_PX, src[2] , src[3] + READ_HORIZ_PX)
return new_src
frame_num = 0
for prospect in os.listdir('./'):
if 'image-' in prospect:
frame_num +=1
if FRAME_SKIP_INTERVAL != -1 and frame_num % FRAME_SKIP_INTERVAL == 0:
continue
frame = Image.open(prospect)
if not dst_img:
dst_img = Image.new(frame.mode, frame.size, "black")
qr_region = frame.crop(CROP_BOX)
dst_img.paste(qr_region, paste_box)
paste_box = increment_paste_box(paste_box)
dst_path = dst_filename + '_' + str(READ_HORIZ_PX) + '_' + str(FRAME_SKIP_INTERVAL) + '.jpeg'
dst_img.save(dst_path, 'JPEG')
print 'saved ' + dst_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment