Labels

Wednesday, June 23, 2010

perl example

Perl Examples

1. Calling System Commands
* Image Processing
* Renaming Files
* File Conversion
* Creating Directories
* Padding & Unpadding Files

2. Scanning the Network
* Finding Free Machines
* Finding Processes
* Finding Files
* Finding Users

3. File Manipulation
* Generating HTML Files
* Generating Xpost Scripts
* Modifying Files
* Convert Raw Data

Remember: In order to be able to run your perl script, it must begin with the line:

#!/usr/local/bin/perl

Furthermore, if you've named the file "myFile", then to make the file executable, you need to type in a unix window:

chmod 755 myFile

Image Processing

#!/usr/local/bin/perl
#
# composite series of images over a background image
#

if ($#ARGV != 4) {
print "usage: compem bg.rgb inbase outbase startNum stopNum\n";
exit;
}

$bg = $ARGV[0];
$inbase = $ARGV[1];
$outbase = $ARGV[2];
$start = $ARGV[3];
$stop = $ARGV[4];

# for each image
for ($i=$start; $i <= $stop; $i++) { # pad numbers $num = $i; if($i<10) num = "00$i" num = "0$i" cmd = "over $bg $inbase.$num $outbase.$num 0 0" old =" $ARGV[0];" new =" $ARGV[1];" start =" $ARGV[2];" stop =" $ARGV[3];" i="$start;" num =" $i;" num = "00$i" num = "0$i" cmd = "mv $old.$num $new.$num" intype =" $ARGV[0];" outtype =" $ARGV[1];" old =" $ARGV[2];" new =" $ARGV[3];" start =" $ARGV[4];" stop =" $ARGV[5];" i="$start;" num =" $i;" num = "00$i" num = "0$i" cmd = "imgcvt -i $intype -o $outtype $old.$num $new.$num" base =" $ARGV[0];" start =" $ARGV[1];" stop =" $ARGV[2];" i="$start;" num =" $i;" num = "00$i" num = "0$i" cmd = "mkdir $base$num" base =" $ARGV[0];" start =" $ARGV[1];" stop =" $ARGV[2];" i="$start;" num =" $i;" num = "00$i" num = "0$i" cmd = "mv $base$i $base$num" cmd = "mv $base$num $base$i" machines =" `systems" sgis =" split(/" sgis =" sort(@sgis);" process =" $ARGV[0];" machines =" `systems" sgis =" split(/" sgis =" sort(@sgis);" lines =" `rsh" line ="~" user =" $1;" pid =" $2;" filename =" $ARGV[0];" dir =" `pwd`;" dir =" $_[0];" lines =" `cd" line ="~" file =" $1;" lines =" `cd" line ="~" line ="~" subdir =" $dir." username =" $ARGV[0];" machines = "insanity " machines =" split(/" machines =" sort(@machines);" base =" $ARGV[0];" num =" $ARGV[1];" i="1;">$base$i.html");

if($i==$num) {
$next = 1;
} else {
$next = $i+1;
}

$base$i print HTML "\n\n\n\n\n";
print HTML "\n";
print HTML "\n\n";

close(HTML);
}


Generating Xpost Scripts

#!/usr/local/bin/perl
#
# generate an xpost script to adjust saturation, and run xpost
#
if ($#ARGV != 2) {
print "usage: fixsat infile.tiff outfile.tiff satval\n";
exit;
}

$infile = $ARGV[0];
$outfile = $ARGV[1];
$satval = $ARGV[2];

# open xpost script
open(XPOST, "&gt__tmp.xp");

# set view to register A
print XPOST "view A\n";

# load original image into reg A
print XPOST "load $infile\n";

# run Kmult to turn down saturation
print XPOST "Kmult $satval $satval $satval 1.0 a b\n";

# set view to register B
print XPOST "view B\n";

# save unsaturated image
print XPOST "save tiff $outfile\n";

# close xpost script
close(XPOST);

# run xpost script
$cmd = "xpost -q -s __tmp.xp";
print $cmd."\n";
system($cmd);

# clean up
$cmd = "/bin/rm -f __tmp.xp";
print $cmd."\n";
system($cmd);


Modifying Text Files

#!/usr/local/bin/perl
#
# change all occurances of a string in a file to another string
#

if ($#ARGV != 3) {
print "usage: chstring oldfile newfile oldstring newstring\n";
exit;
}

$oldfile = $ARGV[0];
$newfile = $ARGV[1];
$old = $ARGV[2];
$new = $ARGV[3];

open(OF, $oldfile);
open(NF, ">$newfile");

# read in each line of the file
while ($line = ) {
$line =~ s/$old/$new/;
print NF $line;
}

close(OF);
close(NF);


Convert Raw Timecode Data to Readable Data

#!/usr/local/bin/perl
#
# Change raw timecode data to different format
#
# timecode data event looks like:
#
# Event: 1
# 00:01:05:23
# 00:01:27:21
# a-2-9
#
# Event: 2
# 00:01:56:13
# 00:02:03:19
# a-3-9
#
# ...and so on...
#
# Want to change it to the form:
#
# a-2-9 = 21.93 seconds = 658 frames
# a-3-9 = 7.20 seconds = 216 frames
#

open(FP,") {

if ($line =~ /^\d\d/ && $first) {
$in = $line;
$first = 0;
} elsif ($line =~ /^\d\d/ && !$first) {
$out = $line;
$first = 1;
} elsif ($line =~ /^\w-/) {
$shot = $line;
chop($shot);

# parse timecodes and
# translate in and out into seconds
$in =~ /(\d\d):(\d\d):(\d\d):(\d\d)/;
$hrs = $1;
$mns = $2;
$scs = $3;
$fms = $4;
$inSecs = $hrs * 3600 + $mns * 60 + $scs + $fms / 30;

$out =~ /(\d\d):(\d\d):(\d\d):(\d\d)/;
$hrs = $1;
$mns = $2;
$scs = $3;
$fms = $4;
$outSecs = $hrs * 3600 + $mns * 60 + $scs + $fms / 30;

# calc duration
$dur = $outSecs - $inSecs;
$total += $dur;

# print line
printf("$shot = %.2f seconds = %d frames\n", $dur, $dur * 30);
}
}

print "total = ".($total / 60)." mins\n";

close FP;


No comments:

Post a Comment