Skip to content

Instantly share code, notes, and snippets.

@ackman678 ackman678/pyMencoder.py
Last active Dec 18, 2015

Embed
What would you like to do?
A python script to utilizing imagemagick and mencoder to convert subfolders of tiff sequences into avi movies. Requires that Imagemagick and mencoder are installed, and in the Windows system search path
#! /usr/bin/env python
#pyMencoder
#a script to utilizing imagemagick and mencoder to convert subfolders of tiff sequences into avi movies. Requires that Imagemagick and mencoder are installed, and in the Windows system search path
#James Ackman March 3, 2010
import os, sys, glob, re, subprocess
#edit the following line for the location of the ImageMagick convert program. This is mainly for Windows where a identically named system tool for converting FAT32 to NTFS is located at c:\Windows\system32\convert.exe and can cause issues, even if ImageMagick directory is located first in your system path.
IMconvert = 'C:\Program Files\ImageMagick-6.6.0-Q16\convert.exe'
#use following several lines with a loop through dirlist for recursive renaming of files
rootdir=os.getcwd()
dirlist=os.listdir(rootdir)
#print dirlist
for dirname in dirlist:
if os.path.isdir(dirname):
os.chdir(dirname)
filelist = glob.glob('*.tif')
#print flist1
try:
for fname in filelist:
newfname=fname[:-4] + '.jpg'
p1=subprocess.Popen([IMconvert,fname,newfname])
p1.communicate()
print newfname
p2=subprocess.Popen(['mencoder','mf://*.jpg','-mf','fps=22.5','-ovc','lavc','-lavcopts','vcodec=mjpeg','-of','lavf','-lavfopts','format=mov','-o','output.mov'])
p2.communicate()
msg = dirname + ' finished!'
print msg + '\n'
except IndexError:
print 'Sorry, nothing to do in folder\n'
os.chdir(rootdir)
#FOR %a in (*.tif) DO convert %a %~dpna.jpg #DOS batch script
#subprocess.Popen(['FOR','%%a','in','(*.tif)','DO',IMconvert,'%%a','%%~dpna.jpg']).communicate()
#FOR /R %%a IN (*.tif) DO convert %%a "%%~dna.jpg" #recursive DOS batch script
##mogrify -format jpg *.tif
##subprocess.call(['mogrify','-format','jpg','*.tif'])
#subprocess.Popen([IMconvert,'*.tif','%06d.jpg']).communicate()
##convert fname fname[0:-4].jpg
##mencoder "mf://*.jpg" -mf fps=22.5 -o output.avi -ovc lavc #compressed ffmpeg4 format, not suitable for imagej stack import
##subprocess.call(['mencoder','mf://*.jpg','-mf','fps=22.5','-o','output.avi','-ovc','lavc'])
##mencoder "mf://*.jpg" -mf fps=22.5 -ovc lavc -lavcopts vcodec=mjpeg -of lavf -lavfopts format=mov -o output.mov #mjpeg format, good for quicktime import in imagej
#must try python for loop with filelist again. But use os.walk() tuple list, for better memory management? Set subprocess calls into functions for precompilations and less for loop thrashing, better speed? Need benchmark return times.
#p1=subprocess.call(['mogrify','-format','jpg','*.tif']) #breaks over slow network connection after several thousand images.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.