Slideshow.py

From ActiveArchives
Jump to: navigation, search

This program uses Python, ImageMagick, and mencoder to convert images into a "slideshow" movie.

links/go.txt;25;25

Source

#!/usr/bin/python
 
from optparse import OptionParser
import os, random, sys
 
parser = OptionParser()
# parser.add_option("-f", "--file", dest="filename", help="write report to FILE", metavar="FILE")
parser.add_option("-W", "--width", dest="width", type="int", help="image width (max)")
parser.add_option("-H", "--height", dest="height", type="int", help="image height (max)")
parser.add_option("-S", "--spf", dest="spf", type="float", help="seconds per frame")
parser.add_option("-O", "--out", dest="out", type="string", help="output filename")
parser.add_option("-X", "--noextraframe", dest="extraframe", action="store_false", help="no blank frame at end")
parser.add_option("--shuffle", dest="shuffle", action="store_true", help="randomize order of images")
parser.set_defaults (
	width=640,
	height=480,
	spf=1.0,
	out="slideshow.avi",
	shuffle=False,
	extraframe=True
)
 
(options, args) = parser.parse_args()
 
# use ImageMagick to resize each
 
if not os.path.isdir("tmp"):
	os.mkdir("tmp")
 
newimgcount = 0
imagetype = None
 
if options.shuffle:
	random.shuffle(args)
 
for arg in args:
	# cmd = """convert "%s" -resize 640x480 "%s" """
	cmd = """convert "%(IN)s" -resize %(W)dx%(H)d\> -size %(W)dx%(H)d xc:black +swap -gravity center -composite "%(OUT)s" """
	(path, base) = os.path.split(arg)
	(base, ext) = os.path.splitext(base)
	if imagetype == None:
		if (ext.lower() == "png"):
			imagetype = "png"
		else:
			imagetype = "jpeg"
	newimgcount+=1
	newimgpath = os.path.join("tmp", ("IMG%04d"%newimgcount)+".JPG")
	cmd %= { 'IN': arg,
			 'OUT': newimgpath,
			 'W' : options.width,
			 'H': options.height }
	print "*", cmd
	# DO IT
	os.system(cmd)
 
# generate an extra black frame
if options.extraframe:
	cmd = """convert -size %(W)dx%(H)d xc:black -type TrueColor "%(OUT)s" """
	newimgcount+=1
	newimgpath = os.path.join("tmp", ("IMG%04d"%newimgcount)+".JPG")
	cmd %= { 'OUT': newimgpath,
			 'W' : options.width,
			 'H': options.height }
	print "*", cmd
	os.system(cmd)
 
cmd = """mencoder mf://tmp/* -mf type=%(TYPE)s:fps=%(FPS)f -o "%(OUT)s" -ovc lavc """
cmd %= { 'FPS': (1.0/options.spf),
         'OUT': options.out,
		 'TYPE': imagetype }
print "*", cmd
os.system(cmd)
 
# cleanup
os.system("rm tmp/*")
os.rmdir("tmp")

Notes

The automagically generated help is a bit lacking as it doesn't mention that the final parameter (and most important!) are the actual images to use in the slideshow! (how to fix!?) A nice extension would be to use the metadata to generate an accompanying SRT file (perhaps just showing selected data like date/time taken?).

What links here

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox