| 1 | = resize_orthos.pl = |
| 2 | The resize_orthos.tar.gz is not available because I can't upload it. It is not required for the script to work, only to run the example. -sw http://www.highwayengineer.co.medina.oh.us/resize_orthos.tar.gz |
| 3 | |
| 4 | {{{ |
| 5 | #!perl |
| 6 | #!/usr/bin/perl -w |
| 7 | # |
| 8 | # Copyright (C) 2002, Lowell Filak. |
| 9 | # You may distribute this file under the terms of the Artistic |
| 10 | # License. |
| 11 | # |
| 12 | # Given a directory containing tiff(s) and tfw(s), the original pixel size, |
| 13 | # the destination directory for resized & recolored tiff(s), & the new |
| 14 | # pixel size this routine will resize & recolor the tiff(s) and write |
| 15 | # new wld files to match. |
| 16 | # |
| 17 | # Required modules are Getopt (normally included with Perl). |
| 18 | # A successful install of ImageMagick |
| 19 | # is required. The routine does NOT utilize the ImageMagick perl module |
| 20 | # at this time. The routine also assumes a *n*x system, please change |
| 21 | # command lines accordingly. |
| 22 | # Please download resize_orthos.tar.gz also, and: |
| 23 | # tar -xf resize_orthos.tar.gz --ungzip |
| 24 | # |
| 25 | # Suggested run line = ./resize_orthos.pl -orig=orthos -osize=8 -out=new -nsize=10 |
| 26 | ################################################################################ |
| 27 | use Getopt::Long; |
| 28 | # |
| 29 | # Subroutine for bailout on error. |
| 30 | sub bailout { |
| 31 | print "*** HELP! I've Fallen & I Can't Get Up!\n"; |
| 32 | # |
| 33 | # Throw marker in output message. |
| 34 | print "*********************************************************************\n"; |
| 35 | # |
| 36 | # Print date & time. |
| 37 | system("date"); |
| 38 | exit 0; |
| 39 | } # end subroutine |
| 40 | ################################################################################ |
| 41 | # |
| 42 | # Main routine. |
| 43 | # |
| 44 | # Grab input values. |
| 45 | &GetOptions('orig=s' => \$orthos_orig, 'osize=s' => \$orig_size, 'out=s' => \$orthos_out, 'nsize=s' => \$new_size); |
| 46 | # |
| 47 | # Check the input values. |
| 48 | if ( (!$orthos_orig) || (!$orig_size) || (!$orthos_out) || (!$new_size) ) { |
| 49 | print "Syntax: resize.pl -orig=[original_dir] -osize=[original_pixel_size] -out=[out_dir] -nsize=[new_pixel_size]\n"; |
| 50 | bailout; |
| 51 | } |
| 52 | # |
| 53 | # Declare variables. |
| 54 | # Note: Please change this to your path to convert (whereis convert). |
| 55 | $convert = '/usr/X11R6/bin/convert'; |
| 56 | # |
| 57 | # Make sure out directory exists. |
| 58 | # |
| 59 | # Split the path apart. |
| 60 | @mpath = split('/',$orthos_out); |
| 61 | # |
| 62 | # How many members to the path. |
| 63 | $mpathcnt = scalar @mpath; |
| 64 | # |
| 65 | # Is the first member blank, ie. ' '/home/global . |
| 66 | # Basically was an absolute or relative path specified. |
| 67 | if (!$mpath[0]) { |
| 68 | # |
| 69 | # Start with 1. |
| 70 | $start = 1; |
| 71 | # |
| 72 | # Start path with /. |
| 73 | $cpath = '/'; |
| 74 | } |
| 75 | else { |
| 76 | # |
| 77 | # Start with 0. |
| 78 | $start = 0; |
| 79 | # |
| 80 | # Start path with ''. |
| 81 | $cpath = ''; |
| 82 | } |
| 83 | # |
| 84 | # End with total -1. |
| 85 | $mpathcnt = $mpathcnt - 1; |
| 86 | for $pathpart ($start .. $mpathcnt) { |
| 87 | # |
| 88 | # Set the create path. |
| 89 | $cpath = $cpath . $mpath[$pathpart]; |
| 90 | if ( -e "$cpath" ) { |
| 91 | # |
| 92 | # Do nothing. |
| 93 | } |
| 94 | else { |
| 95 | # |
| 96 | # Create it. |
| 97 | @mkdirerror = system("mkdir $cpath"); |
| 98 | if ( $mkdirerror[0] > 0 ) { |
| 99 | print "*** Mkdir $cpath Failed!\n"; |
| 100 | bailout; |
| 101 | } |
| 102 | else { |
| 103 | # |
| 104 | # Fall through. |
| 105 | } |
| 106 | } |
| 107 | # |
| 108 | # For lack of a better term "Increment" path. |
| 109 | $cpath = $cpath.'/'; |
| 110 | } |
| 111 | # |
| 112 | # Append the new size to the out dir to allow for multiple new sizes. |
| 113 | $orthos_out = $orthos_out . "/" . "$new_size"; |
| 114 | # |
| 115 | # Print date & time just for information. |
| 116 | #@dateerror = system("date"); |
| 117 | # |
| 118 | # Create the destination directory. |
| 119 | @mkdirerror = system("mkdir $orthos_out"); |
| 120 | # |
| 121 | # We know the original pixel size is $orig_size so use that to determine |
| 122 | # the percentage of reduction for the new size. |
| 123 | # Notes: Try to keep this so the division comes out evenly. Uneven results |
| 124 | # can leave black lines between tiles. This may only work for flat |
| 125 | # projections. |
| 126 | my $percentage = $orig_size / $new_size * 100; |
| 127 | # |
| 128 | # Check the percentage. |
| 129 | if ( $percentage > 100 ) { |
| 130 | print "Argh! I have no idea what the output will look like if I resample smaller.\n"; |
| 131 | bailout; |
| 132 | } |
| 133 | else { |
| 134 | # |
| 135 | # Fall through. |
| 136 | } |
| 137 | # |
| 138 | # Create a list of files to convert. |
| 139 | system("ls -1 $orthos_orig/*.tif > orthos.list"); |
| 140 | # |
| 141 | # Read-in and loop through each ortho. |
| 142 | # |
| 143 | # Open the list. |
| 144 | open(ORTHOS, "<orthos.list"); |
| 145 | # |
| 146 | # Loop. |
| 147 | while(<ORTHOS>) { |
| 148 | # |
| 149 | # Split the full path apart. |
| 150 | # |
| 151 | # Grab the line. |
| 152 | my $full_path = $_; |
| 153 | # |
| 154 | # Remove the newline. |
| 155 | $full_path =~ s/\n//; |
| 156 | # |
| 157 | # Split on the /. |
| 158 | my @full_path = split(/\//, $full_path); |
| 159 | # |
| 160 | # How many parts to the path. |
| 161 | my $path_length = scalar(@full_path); |
| 162 | # |
| 163 | # Bring the count down one to match array start of 0. |
| 164 | $path_length = $path_length - 1; |
| 165 | # |
| 166 | # File name is the last member of array. |
| 167 | my $file = $full_path[$path_length]; |
| 168 | # |
| 169 | # Chop off file extension. |
| 170 | my @file = split(/\./, $file); |
| 171 | # |
| 172 | # Set world file name to just name without extension. |
| 173 | my $tfw = $file[0]; |
| 174 | # |
| 175 | # Create temp & out file name. |
| 176 | my $tmp_out = "/tmp/" . $file; |
| 177 | my $file_out = $orthos_out . "/" . $file; |
| 178 | # |
| 179 | # See if tiff already exists. |
| 180 | if ( -e "$file_out") { |
| 181 | # |
| 182 | # Do nothing. |
| 183 | } |
| 184 | else { |
| 185 | # |
| 186 | # Convert the resized tif image into /tmp. |
| 187 | my $convert_line = $convert . " -geometry " . $percentage . "\%x" . $percentage . "\% " . $full_path . " " . $tmp_out; |
| 188 | # |
| 189 | # For debugging only. |
| 190 | print "$convert_line\n"; |
| 191 | # |
| 192 | # run the resize convert statement. |
| 193 | @converterror = system("$convert_line"); |
| 194 | # |
| 195 | # Convert the recolored tif image into $orthos_out. |
| 196 | # Note: This currently allows for 256 colors. It has been reported that |
| 197 | # this number must be smaller to allow for antialiased fonts. I |
| 198 | # have not experienced this directly but if it does create a |
| 199 | # problem change the 256 to a lower number. |
| 200 | $convert_line = $convert . " -colors 256 " . $tmp_out . " " . $file_out; |
| 201 | # |
| 202 | # For debugging only. |
| 203 | print "$convert_line\n"; |
| 204 | # |
| 205 | # run the resize convert statement. |
| 206 | @converterror = system("$convert_line"); |
| 207 | # |
| 208 | # Remove temporary resized file. |
| 209 | unlink $tmp_out; |
| 210 | } |
| 211 | # |
| 212 | # Create original & out world file names. |
| 213 | # Note: In this case the original images are tfw instead of world files. |
| 214 | my $tfw_in = $orthos_orig . "/" . $tfw . ".tfw"; |
| 215 | my $tfw_out = $orthos_out . "/" . $tfw . ".wld"; |
| 216 | # |
| 217 | # See if tfw already exists. |
| 218 | if ( -e "$tfw_out") { |
| 219 | # |
| 220 | # Do nothing. |
| 221 | } |
| 222 | else { |
| 223 | # |
| 224 | # Open the existing tfw for reading to create the new tfw. |
| 225 | # |
| 226 | # For debugging only. |
| 227 | #print "$tfw_in\n"; |
| 228 | # |
| 229 | # Open it. |
| 230 | open(TFW, "<$tfw_in"); |
| 231 | # |
| 232 | # Open new tfw for writing. |
| 233 | # |
| 234 | # For debugging only. |
| 235 | #print "$tfw_out\n"; |
| 236 | # |
| 237 | # Open it. |
| 238 | open(TFWOUT, ">$tfw_out"); |
| 239 | # |
| 240 | # Set a line counter. |
| 241 | my $linenum = 1; |
| 242 | # |
| 243 | # Loop through each line. |
| 244 | while(<TFW>) { |
| 245 | # |
| 246 | # Grab the line. |
| 247 | my $line = $_; |
| 248 | # |
| 249 | # Remove leading spaces. |
| 250 | $line =~ s/^\s+//; |
| 251 | # |
| 252 | # Remove newline, carriage return, or both. |
| 253 | $line =~ s/\015\012|\015|\012//g; |
| 254 | # |
| 255 | # For debugging only. |
| 256 | #print "$line\n"; |
| 257 | # |
| 258 | # Is it line #1 or #4. |
| 259 | if ( $linenum == 1 ) { |
| 260 | # |
| 261 | # If so print the new pixel size (x). |
| 262 | print TFWOUT "$new_size\.000000\n"; |
| 263 | } |
| 264 | elsif ( $linenum == 4 ) { |
| 265 | # |
| 266 | # If so print the new pixel size negative (y). |
| 267 | print TFWOUT "-$new_size\.000000\n"; |
| 268 | } |
| 269 | else { |
| 270 | # |
| 271 | # Else just dump the existing line. |
| 272 | print TFWOUT "$line\n"; |
| 273 | } |
| 274 | # |
| 275 | # Increment the line counter. |
| 276 | $linenum = $linenum + 1; |
| 277 | } |
| 278 | # |
| 279 | # Close the input and output world files. |
| 280 | close TFWOUT; |
| 281 | close TFW; |
| 282 | } |
| 283 | } |
| 284 | # |
| 285 | # Close the orthos list. |
| 286 | close ORTHOS; |
| 287 | # |
| 288 | # Remove the orthos list. |
| 289 | unlink "orthos.list"; |
| 290 | }}} |
| 291 | ---- |
| 292 | back to PerlMapScript |