A Perl script to automate image sorting and PTBatch template file creation
IVRPA Workflow Automation Panel, Berkeley, June 19, 2007
A Perl script to move and rename sets of panorama files and then create custom PTMac configuration files for use with PTBatch and CubicConverter.
Ken Stuart, ken@SirRound.us
Premise: The use of a high-precision panorama head facilitates the batch processing of the stitching process, but it can be painfully tedious to group sets of files into those used for creating a particular panorama, especially when shooting bracketed sets of images at each node. Further, using a precisely calibrated panorama rig means that a single configuration file can be used for all nodes' control points, but editing the configuration file to use different sets of images is also highly tedious.
Solution: A script that accepts simple parameter values and then moves and groups individual images files for each panorama, creating and naming a folder to hold them, automatically specifying their bracket. Additionally, the same script will modify a sample configuration file and save it separate for each panorama as it handles the sets of images.
The process:
- Copy all files from camera media to computer
- Remove any images not used in the panoramas (e.g., stills, extra frames, partial panoramas)
- Prepare images if necessary (e.g., rotate, apply alpha masks)
- Open the script and specify variables
- Paths to source and destination folder, and to PTMac template configuration file
- Number of brackets
- Name of brackets
- Number of nodes
- Number of frames per node
- Names of nodes
- Save changes to script
- Execute the script from the Terminal command line
- All files are now sorted into named folders and each has a PTMac configuration file
- Load PTMac files into PTBatch to generate sets of cube faces and then edit those as needed (or go right to QTVRs for draft panos)
- Load the edited cube face sets into CubicConverter's batch process to generate equirectangular images and then sharpen those as needed
- Load above results into CubicConverter's batch process to generate QTVR files
How to make this possible:
- Use a calibrated high-precision panorama rig
- Be consistent with your process
- always shoot same direction
- sort out bracketed vs. non-bracket sets before running the script
- always shoot nadir even if it will be easier to Photoshop it in
- always shoot nadir with or without bracketing, to match regular frames
- Ensure Perl is on your system (download and install if needed; it's free)
- Need to install special module (MacPerl) in order to re-type Mac files, i.e. convert text files into PTMac files
Perl code follows below. Comments start with an octothorpe (#), except the first line, which is best left as-is. Copy this code in to a text editor, save it (use .pl for an extension), and run it via the command line interface by typing "perl " and then the name of the new file. Be sure to change the variables for your number of nodes, their names, and the numbers and names of brackets if used. For further assistance, contact the author at the email address above.
#!/usr/bin/perl
#Created by Ken Stuart, ken@SirRound.us, copyright 2007. Permission to use and modify for any constructive purpose is granted. Permission to repost, distribute, sell, repackage, etc., only with express written consent of the author. If in doubt, ask first.
# This script takes sets of pano image files and sorts them into folders and bracketed sets, saving a lot of time compared to manually creating new folders, naming them, and then moving files from the original to the sets. The specified folder must contain a complete sets of files, i.e., if there are 10 nodes and each node consists of 8 shots, each of which has 3 bracketed images, then the folder must contain 240 images exactly.
# This script also loads a PTMac template file, modifies it to use the images files, and saves it for use in PTBatch.
use strict;
use MacPerl; # Used to set file type and creator for output template file, so that PTBatch recognizes it.
# Path and name of folder with pano images, e.g. /Users/jdoe/Desktop/Panos/originals
# Hint: to find a path, open Terminal and drag a folder into it.
my $StartingFolder = "/Users/kenstuart/Desktop/IVRPA/SanFrancisco";
# Path and name of folder to store sorted images (can be same as above).
my $EndingFolder = "/Users/kenstuart/Desktop/IVRPA/SanFrancisco";
# Path and name of PTMac template file.
my $PTMacTemplateFile = "/Users/kenstuart/Desktop/template.ptmac";
# Number of images per pano, usually 4-8 + 1, including top and bottom and nadir.
my $NumberOfImagesPerPano = 9;
# Number of frames per shot, usually 3 if bracketing, or 1 if no bracketing.
my $NumberOfBrackets = 1;
# Names, in order, that you want to call bracketed sets, e.g., normal, dark, light.
# You should have the same number of terms as number of brackets; if no brackets, you may wish to have a generic term.
#my @BracketNames = ("Normal", "Dark", "Light");
my @BracketNames = ("Auto");
# Number of nodes shot.
my $NumberOfNodes = 3;
# Enter a set of names for the folders that will contain each node, e.g., apse, nave, northaisle, southaisle.
# If you leave the list of node names blank, the script will assign them names in the format "Node_n" where n is the number of the node.
#my @NodeNames = ("A", "B", "C");
my @NodeNames = ("Wharf", "Fountain", "Alcatraz");
my $number = 0;
if ($#NodeNames == -1) {
for (my $i=1; $i<=$NumberOfNodes; $i++) {
if ($i < 10) {$number = "0".$i;} else {$number = $i;}
push(@NodeNames, "Node_".$number);
}
}
# Enter file extension used to in image filenames, usually either .jpg or .tif.
my $ImageFileNameExt = ".tif";
# Load PTMac template file into memory.
open(TEMPLATE, $PTMacTemplateFile) || die ("Could not find starting folder $PTMacTemplateFile. Please check its name in this script on and on the machine. Names may be case-sensitive, and should not contain certain special characters such as spaces, slashes or colons. Cannot continue until problem is resolved.\n\n");
my @template = ;
close(TEMPLATE);
my $template = "";
foreach my $line (@template) {$template .= $line;}
# Check starting folder.
unless (-e $StartingFolder) {print "Could not find starting folder $StartingFolder. Please check its name in this script on and on the machine. Names may be case-sensitive, and should not contain certain special characters such as spaces, slashes or colons. Cannot continue until problem is resolved.\n\n"; exit;}
# Load image files into a list and sort it.
opendir (DIRLIST, $StartingFolder) || die print "Cannot open directory $@.";
my @ImageFiles = grep {/$ImageFileNameExt$/i} readdir(DIRLIST);
@ImageFiles = sort(@ImageFiles);
close (DIRLIST);
# Confirm correct number of images in directory.
my $neededImages = $NumberOfImagesPerPano * $NumberOfBrackets * $NumberOfNodes;
my $imageCount = $#ImageFiles+1;
print "Number of images needed: $neededImages; number found in $StartingFolder: $imageCount\n";
if ($neededImages != $imageCount) {
print "Cannot continue. Please check numbers and image set. (Were the JPGs converted to TIFs?)\n\n";
exit;
}
# Make destination folder.
unless (-e $EndingFolder) {mkdir $EndingFolder;}
# Make new folders for each node and each bracket and move images to them.
# Also, create PTMac template file for each node/bracket.
my $count = 0;
my %templates;
my $templateFileName = "";
my $templateStr = "";
foreach my $nodeName (@NodeNames) {
for (my $i=0; $i<$NumberOfImagesPerPano; $i++) {
unless (-e "$EndingFolder/$nodeName") {mkdir "$EndingFolder/$nodeName";}
foreach my $bracketName (@BracketNames) {
unless (-e "$EndingFolder/$nodeName/$bracketName") {
mkdir "$EndingFolder/$nodeName/$bracketName";
$templates{$bracketName} = $template;
}
print "Moved $StartingFolder/@ImageFiles[$count] to $EndingFolder/$nodeName/$bracketName/@ImageFiles[$count]\n";
rename ("$StartingFolder/@ImageFiles[$count]", "$EndingFolder/$nodeName/$bracketName/@ImageFiles[$count]");
$templateStr = "$EndingFolder/$nodeName/$bracketName/@ImageFiles[$count]";
$templateStr =~ s#/#:#g;
$templates{$bracketName} =~ s/path:filename/$templateStr/is;
$count++;
}
}
while((my $key, my $value) = each(%templates)) {
# Save template file.
$templateFileName = "$EndingFolder/$nodeName"."_"."$key.ptmac";
open(TEMPLATE, ">$templateFileName");
print TEMPLATE $value;
close (TEMPLATE);
MacPerl::SetFileInfo("PTMC", "TEXT", $templateFileName);
}
}
print "Done\n\n\n";
# Script ends here.


Re: A Perl script to automate image sorting and PTBatch template
Submitted by patrickcheatham on Wed, 2007-07-18 23:27.Hey Ken:
Neat! Thanks for posting the script...
Do you have good links to Perl and MacPerl downloads?
Thanks!
Patrick Cheatham
--
CheathamLane | spinControl:VR
Berkeley, California
VR Photography
Web, Flash & QuickTime Development
Re: A Perl script to automate image sorting and PTBatch template
Submitted by Ken Stuart on Thu, 2007-07-19 22:19.Sure.
MacPerl: http://www.macperl.com/
Perl: http://www.perl.com/
You should not need regular Perl if you have a Mac because it's built in to MacOS. You need the MacPerl part installed if you want the script to be able to assign the correct Macintosh filetype to the PTMac templates created for each panorama.
-Ken
Re: A Perl script to automate image sorting and PTBatch ...
Submitted by sam22 on Sun, 2009-12-20 16:26.Great! I was looking for that script.
------------
Sam, photo restoration services.