Jp2 batch converter

From OpenSimulator

(Difference between revisions)
Jump to: navigation, search
(Code)
Line 16: Line 16:
 
  perl jpg2jp2.pl
 
  perl jpg2jp2.pl
  
==Code==
+
  #!/usr/bin/perl -s -U
######################################
+
  # textureprep.pl by Phrearch
#
+
# jpg2jp2.pl
+
#
+
# by: Phrearch
+
#
+
# Easily converts a batch of jpg files
+
# to jp2 files...
+
######################################
+
  #!/usr/bin/perl
+
  $iteration=1;
+
 
   
 
   
  foreach my $file(`ls *.jpg`) {
+
  use Cwd;
chop($file);
+
$dirs=1;
system("jasper --input $file --output $iteration.jp2 -T jp2;");
+
$files=0;
$iteration++;
+
}
+
sub ScanDirectory{
 +
    my ($workdir) = shift;
 +
 +
    my ($startdir) = &cwd; # keep track of where we began
 +
 +
    chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
 +
    opendir(DIR, ".") or die "Unable to open $workdir:$!\n";
 +
    my @names = readdir(DIR) or die "Unable to read $workdir:$!\n";
 +
    closedir(DIR);
 +
 +
    foreach my $name (@names){
 +
        next if ($name eq ".");
 +
        next if ($name eq "..");
 +
 +
#What? a directory?!!
 +
        if (-d $name){
 +
$dirs++;
 +
&ScanDirectory($name);
 +
next;
 +
        }
 +
#We found one of those nasty textures
 +
        if (substr($name, -4) eq ".jpg") {
 +
print "Processing $workdir\\$name...";
 +
$rawname = substr $name, 0, -4;
 +
system("convert -format tga $rawname.jpg $rawname.tga &>/dev/null");
 +
system("rm $name &>/dev/null");
 +
system("image_to_j2k -i $rawname.tga -o $rawname.j2k -r 20,10,1 &>/dev/null");
 +
system("mv $rawname.j2k $rawname.jp2 &>/dev/null");
 +
system("rm $rawname.tga &>/dev/null");
 +
print "done!\n";
 +
$files++;
 +
        }
 +
        chdir($startdir) or
 +
          die "Unable to change to dir $startdir:$!\n";
 +
    }
 +
}
 +
&ScanDirectory(".");
 +
print "\nDirectories scanned:$dirs\nFiles processed:$files\n";

Revision as of 05:28, 21 February 2008

Description

The jp2 files used by the Secondlife client, are actually j2k files. These can be encoded by image_to_j2k from openjpeg. The script checks for available tif-files in a root directory, and compresses those to j2k and renames them...

Usage

  • Install OpenJPEG svn (if you have trouble compiling, you could try the binaries on their site)
svn co http://www.openjpeg.org/svn/trunk
cd trunk
mkdir bin
cd bin
cmake .. -DBUILD_EXAMPLES:BOOL=ON
make
make install
  • Make sure that the binaries image_to_j2k and j2k_to_image can be executed from anywhere
  • Put the code in a file named jpg2jp2.pl, and copy it to the directory where your jpg's are. Call the script:
perl jpg2jp2.pl
#!/usr/bin/perl -s -U
# textureprep.pl by Phrearch

use Cwd;
$dirs=1;
$files=0; 

sub ScanDirectory{
   my ($workdir) = shift;

   my ($startdir) = &cwd; # keep track of where we began

   chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
   opendir(DIR, ".") or die "Unable to open $workdir:$!\n";
   my @names = readdir(DIR) or die "Unable to read $workdir:$!\n";
   closedir(DIR);

   foreach my $name (@names){
       next if ($name eq "."); 
       next if ($name eq "..");

#What? a directory?!!

       if (-d $name){ 

$dirs++; &ScanDirectory($name); next;

       }

#We found one of those nasty textures

       if (substr($name, -4) eq ".jpg") {

print "Processing $workdir\\$name..."; $rawname = substr $name, 0, -4; system("convert -format tga $rawname.jpg $rawname.tga &>/dev/null"); system("rm $name &>/dev/null"); system("image_to_j2k -i $rawname.tga -o $rawname.j2k -r 20,10,1 &>/dev/null"); system("mv $rawname.j2k $rawname.jp2 &>/dev/null"); system("rm $rawname.tga &>/dev/null"); print "done!\n"; $files++;

       }
       chdir($startdir) or 
          die "Unable to change to dir $startdir:$!\n";
   }
}
&ScanDirectory(".");
print "\nDirectories scanned:$dirs\nFiles processed:$files\n";
General
About This Wiki