Skip to content

Instantly share code, notes, and snippets.

@sandyjmacdonald
Created April 10, 2012 14:29
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 sandyjmacdonald/2351759 to your computer and use it in GitHub Desktop.
Save sandyjmacdonald/2351759 to your computer and use it in GitHub Desktop.
Short Python program to create movie barcodes.
#!/usr/bin/env python
# Takes a bunch of screenshots from a movie and converts them into a movie barcode.
# You need to capture the screenshots with the command line version of VLC, which should in OS X,
# Windows and Linux (I think). The command (in a terminal) to do this is:
# vlc /path/to/movie/file --video-filter=scene --scene-prefix=movie --scene-ratio=90 --scene-path=/path/to/folder/to/store/images/
# Note that it plays the movie in real time, so it takes a while.
# You might need to create an alias to vlc with: alias vlc='/Applications/VLC.app/Contents/MacOS/VLC'
# You'll probably also want to calculate a scene-ratio specific for the movie that you're capturing
# screenshots from, so that you don't waste space or have too few screenshots. The scene-ratio tells
# VLC how often to capture an image e.g. every 90 frames for the example above. Assume that there are
# 24 frames per second. Take the length of the movie in minutes, multiply by 60, multiply by 24, then
# divide by 1280, and use this number for the scene-ratio.
# NOTE. You need to have the Python Image Library (PIL) installed for this program to work.
# And this program should be in the same folder as your captured screenshots.
# When you've done all that, run the program by typing 'python barcode.py', sit back, and wait for
# your beautiful movie barcode.
# glob import for listing the thumbnails.
# PIL for processing the thumbnails, and creating the barcode.
import os
import glob
from PIL import Image
# Creates a list of the thumbnails.
thumbs = glob.glob('movie*.png')
i = 1
# Loops through the thumbnails and resizes them into 1 pixel wide slices.
for thumb in thumbs:
try:
print 'converting thumb', str(i), 'of', str(len(thumbs))
im = Image.open(thumb)
h, w = im.size
slice = im.resize((1, h))
slice.save('slice' + str(i) + '.png')
i += 1
except IOError:
print 'could not open', thumb
# Creates a list of the slices we just made.
slices = glob.glob('slice*.png')
j = 0
# Calculates width and height of image. Creates a new image to paste the slices into.
w = len(slices)
h = int(w/2.667)
barcode = Image.new('RGB', (w, h))
# Loops through slices. Converts to single colour. Pastes into image from left to right.
for slice in slices:
print 'pasting slice', str(j + 1), 'of', str(w)
im = Image.open(slice)
pixel = im.resize((1, 1))
newslice = pixel.resize((1, h))
barcode.paste(newslice, (j, 0))
j += 1
# Resizes and saves the image.
barcode = barcode.resize((1280, 480))
barcode.save('barcode.jpg', 'JPEG')
# Cleans up and removes all of the files (apart from the barcode!)
# Comment out this next section if you want to keep the images for later.
print 'cleaning up'
for thumb in thumbs:
os.remove(thumb)
for slice in slices:
os.remove(slice)
print 'processing complete'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment