#!/usr/bin/perl #************************************************************************* # This program uses Linux GIMP to cut out smaller pieces from a large # image, adjust the transfer curve and save as a JPEG with choosable # quality. Output files get numbered sequentially with given base, # starting with 0, increasing by column, top left to lower right. # Program needs to be called as # # crop_and_save.pl IPATH OUTPRE XSIZE YSIZE XN YN XOOFF YOOFF IOFF JOFF XOFF YOFF QUAL TRANS # # with # IPATH, input file # OUTPRE, output filename prefix # XSIZE, size in x of output files # YSIZE, size in y of output files # XN, number of images to be clipped in x # YN, number of images to be clipped in y # XOOFF, naming numbering offset in x (first image has number XOOFF+YOOFF*XN, I think...; this is JUST numbering) # YOOFF, naming numbering offset in y # IOFF, starting not at i=0 but ioff (real skipping of some area while keeping numbering) # JOFF, starting not at j=0 but joff # XOFF, skipping xoff pixel off top (skipping this much pixels) # YOFF, skipping yoff pixel off left # QUAL, JPEG saving quality, 0.0 to 1.0 (80% should be good) # TRANS, name of transfer curve # # Transfer curve maps input brightness value to output brightness value # -lin: linear (no change) # -lin30: linear but setting 30 (of 256) lowest values to black # -lin50: as above but lowest 50 values # define any other curve as a 256 value array with transfer values # from 0th to 255 brightness level # # Author: Knud Jahnke, # Astrophysikalisches Institut Potsdam # http://www.aip.de/~jahnke/ # # Version 1.0, March 2004 # # Bugs: # -absolutely no error handling, it just dies the ugly way... # -crashes when accessing pixels beyond image borders #************************************************************************* use Getopt::Std; getopts('f:'); # **** Activate Gimp usage *********** use Gimp; # ************************************ my @args=(); if($opt_f){ open(FI,"<$opt_f") or die "invalid file\n"; @args=; close(FI); for my $l (@args){chomp($l);$l=~ s/\s*\#.*$//} }else{ my $nn=$#ARGV; if($nn<=13){ print "Call with \n\t\"$0 ipath outpre xsize ysize xn yn xooff yooff ioff joff xoff yoff qual trans\"\nor\t $0 -f \n "; exit(0); } # **** Read command line input ******* @args=@ARGV; } my ($ipath, $outpre, $xsize, $ysize, $xn, $yn, $xooff, $yooff, $ioff, $joff, $xoff, $yoff, $qual, $trans) = @args; if (! -e $ipath){ print "No vaild input-graphics found \n"; exit(0); } # ************************************ # **** Define transfer curves ******** # Linear my @lin; for ($i=0;$i<256;$i++) {$lin[$i] = $i;}; # Linear but set to 0 from 0--50 my @clip50 = @lin; my $zerolim = 50; for ($i=0;$i<$zerolim;$i++) { $clip50[$i] = 0; } $clip50[0] = 1; # Linear but set to 0 from 0--30 my @clip30 = @lin; $zerolim = 30; for ($i=0;$i<$zerolim;$i++) { $clip30[$i] = 0; } $clip30[1] = 1; # ************************************ # **** Start a Gimp session ********** Gimp::init; # ************************************ # **** deciding on transfer curve **** if ($trans eq "clip30") { @transcurve = @clip30; } elsif ($trans eq "clip50") { @transcurve = @clip50; } else { @transcurve = @lin; } # ************************************ # **** Load image ******************** my $img_tile = Gimp->file_load ($ipath, $ipath); # ************************************ #**** loop over a tile **** for ($i=$ioff;$i<$xn;$i++) { for ($j=$joff;$j<$yn;$j++) { $joff=0; #opening is time consuming, save a copy of image in memory my $img = Gimp::gimp_channel_ops_duplicate($img_tile); #define cropping offsets and image name my $xclipoff = $i*$xsize+$xoff; my $yclipoff = $j*$ysize+$yoff; my $iman = ($i+$xooff)*($yn+$yooff) + $j + $yooff; my $oname = $outpre . $iman . '.jpg'; #Say something to the user about progress print "Xclipoff = ", $xclipoff, ",\tYclipoff = ", $yclipoff, ",\tIma #", $iman, "\n"; # crop Gimp->gimp_crop($img,$xsize,$ysize,$xclipoff,$yclipoff); my $layer = ($img->get_layers)[0]; #get first layer # Changing background Gimp::gimp_curves_explicit($layer, 0, 256, \@transcurve); # Save image Gimp::file_jpeg_save(1, $img, $layer, $oname, $oname, $qual, 0, 1, 0, 'have a nice day!', 1, 1, 0, 1); # Delete working image copy from memory to free space $img->delete; } } #Delete also other copy and exit $img_tile->delete;