Jp2 batch converter for windows

From OpenSimulator

Revision as of 10:46, 20 August 2008 by JeroenWaisMan (Talk | contribs)

Jump to: navigation, search

Contents

Description

For how, why and where it is made for: please refer to the wikipage of the original script.

This is a modification of that script to make the system commands work on windows systems. Also a helpful /help explanation and optional /D command to delete the old files is added.

Note: these are some quick adjustments to the original, it may or may not work, depending on your setup. It did work here.

Usage

Dependencies

  • This script is a perl script, so you first should go and install that. ([1])
  • The ImageMagick executables for windows. ([2])

Please note that the 'convert.exe' executable conflicts with the FAT-to-NTFS batch command of windows. This script will therefor seek a 'imconvert.exe' file in the PATH. Please rename it, or adjust the lines that start with:

system("imconvert ...
  • Openjpegs J2K Codec executables, more specifically: image_to_j2k.exe. ([3])
  • References to all executables in the PATH environment variable.

This can be set trough Right-click My Computer->Properties: Advanced tab->Environment variables.

Arguments

The script takes 2 arguments

  • An optional /D argument, if added the script will also delete the old image files
  • A required directory specified.
  • Or, you could start it with a /help command, which gives about the same info as this page.

Running

Execute this from command prompt, optionally adding a /D argument for also deleting the source images.

perl jp2batch.pl /D "c:\directory\with\images\"

Code

# jp2batch.pl
# OpenSim jp2 texture preparation script (for windows)
# Converts jpg/jpeg/bmp/tga files to valid jp2(jk2) files for SL
# Author: Phrearch, modified by Jeroen Waisman for Windows systems
# Credits: Tedd1, Phrearch
 
$dirs=0;
$files=0;
$delete=0;
 
sub ScanDirectory
{
    my ($workdir) = @_;
    local (@names);
    $dirs++;
    opendir(DIR, $workdir) or die "Unable to open $workdir:$!\n";
    @names = readdir(DIR) or die "Unable to read $workdir:$!\n";
    closedir(DIR);
    foreach my $tmpName (@names)
    {
        my $name = $workdir."\\".$tmpName;
        #\. or \.. in directory
        if ($tmpName =~ /^\.\.?$/)
        {
            next;
        }
        #directory
        elsif (-d $name)
        {
            print "Entering directory $name\n";
            &ScanDirectory($name);
            next;
        }	
        #jpg or jpeg
        elsif ($name =~ /^(.*)\.(jpe?g|bmp|tga|tif|raw)$/)
        {
            $rawname = $1;
            $ext = lc($2);
            $files++;
            print "\n----- Processing $name... -----";
            &Convert($rawname,$ext,$delete);
        }
    }
}
sub Convert
{
    my ($rawname, $ext) = @_;
    my @imageXY = split('x',`identify -format "%wx%h" "$rawname.$ext"`); 
    my $imageX = pop(@imageXY);
    my $imageY = pop(@imageXY);
    my $optX;
    my $optY;
    my $ar;
    my $optar;
 
    #landscape or square, search for optimal X
    if (($imageX >= $imageY) && ($imageX != 0))
    {
        $ar = sprintf("%.4f",$imageY / $imageX);
        $optar =  &supportedAR($ar);
        $optX = &supportedXY($imageX);
        $optY = $optX * $optar;
        #both aspectratio and resolution are correct
        if(($imageX!=$optRes) || ($ar!=$optar))
        {
            print "\nResizing ($imageX\xx$imageY => $optY\xx$optX)... ";
            system("imconvert -resize ${optX}x${optY}! \"$rawname.$ext\" \"$rawname.$ext\"");
            print "Done\n";
        }
    }
    #portrait, search for optimal Y
    elsif($imageY != 0)
    {
        $ar = sprintf("%.4f",$imageX / $imageY);
        $optar =  &supportedAR($ar);
        $optY = &supportedXY($imageY);
        $optX = $optY * $optar;
        #Needs conversion
        if(($imageY!=$optRes) || ($ar!=$optar))
        {
            print "\nResizing ($imageX\xx$imageY => $optX\xx$optY)... ";
            system("imconvert -resize ${optX}x${optY}! \"$rawname.$ext\" \"$rawname.$ext\"");
            print "Done\n";
        }
    }
 
    if (($ext eq "jpg") || ($ext eq "jpeg"))
    {
        print "\nConverting jpg to valid openjpeg (tga) format to handle... ";
        system("imconvert -format tga \"$rawname.$ext\" \"$rawname.tga\"");
        print "Done\n";
        if($delete==1)
        {
            print "\nDeleting old file... ";
            system("del \"$rawname.$ext\"");
            print "Done\n";
        }
        $ext="tga";
        $delete=2;
    }
    print "\nCreating .jp2 (jpeg2000) file... ";
    system("image_to_j2k -i \"$rawname.$ext\" -o \"$rawname.jp2\" -r 20,10,1");
    print "Done";
    if($delete>0)
    {
        if($delete==1)
        {
            print "\nDeleting old file... ";
            system("del \"$rawname.$ext\"");
            print "Done\n";
        }
        elsif($delete==2)
        {
            print "\nDeleting temporary .tga file... ";
            system("del \"$rawname.$ext\"");
            print "Done\n";
        }
    }
    print "--------------------------------\n";
}
sub supportedAR
{
    my ($ar) = @_;
    my @arReference=(1.0,0.5,0.25,0.125,0.0625);
    my $arOptimal;
    my $arDivTemp=1.0;
    my $arDiv;
    #Get optimal aspect ratio
    foreach (@arReference)
    {
        $arDiv=abs($ar-$_);
        if($arDiv < $arDivTemp)
        {
            $arDivTemp=$arDiv;
            $arOptimal=$_;
        }
    }
    return $arOptimal;
}  
sub supportedXY
{
    my ($res) = @_;
    my @resReference=(64.0,128.0,256.0,512.0,1024.0);
    my $resOptimal;
    my $resDivTemp=4096.0;
    my $resDiv;
    #Get optimal axis resolution
    foreach (@resReference)
    {
        $resDiv=abs($res-$_);
        if($resDiv < $resDivTemp)
        {
            $resDivTemp=$resDiv;
            $resOptimal=$_;
        }
    }
    return $resOptimal;
} 
if ( @ARGV > 1 )
{
    if((@ARGV[0] eq "/D") || (@ARGV[0] eq "/d"))
    {
        print "Processing ".@ARGV[1]."... (deleting old files) \n";
        $delete = 1;
        &ScanDirectory(@ARGV[1],1);
        print "\nDirectories scanned:$dirs\nFiles processed:$files\n";
    }
    elsif((@ARGV[1] eq "/D") || (@ARGV[1] eq "/d"))
    {
        print "Processing ".@ARGV[0]."... (deleting old files) \n";
        $delete = 1;
        &ScanDirectory(@ARGV[0]);
        print "\nDirectories scanned:$dirs\nFiles processed:$files\n";
    }
    else
    {
        print "Syntax error\n";
        print "Provide just one directory argument, optionally with a /D argument...\n";
    }
}
elsif ( @ARGV > 0 )
{
    if((@ARGV[0] eq "help") || (@ARGV[0] eq "?"))
    {
        print "\nBatch convert images to opensim-compatible .jp2 files\n\n\n";
        print "perl jp2batch.pl [/D] [station:][path]dir\n\n\n";
        print "  dir       Directory (and sub-directories) to search for image files.\n";
        print "  -D        If provided, old files will be deleted.\n\n\n\n\n";
        print "Dependencies, these executables should be available in the PATH or directory of the script.\n\n";
        print "  ImageMagick library functions: (note that I renamed one executable)\n";
        print "  Available at http://www.imagemagick.org/\n\n";
        print "    imconvert.exe       originaly named convert.exe, but renamed due to a Windows batch-command conflict.\n";
        print "    identify.exe\n\n\n";
        print "  OpenJpeg2000 library's J2K Codec: \n\n";
        print "  Available at http://www.openjpeg.org/\n";
        print "    image_to_j2k.exe\n\n\n";
    }
    elsif((@ARGV[1] eq "/D") || (@ARGV[1] eq "/d"))
    {
        print "Syntax error\n";
        print "You must provide a directory to process, optionally with a /D argument...\n";
    }
    else
    {
        print "Processing ".@ARGV[0]."...\n";
        $delete = 0;
        &ScanDirectory(@ARGV[0]);
        print "\nDirectories scanned:$dirs\nFiles processed:$files\n";
    }
}
else
{
    print "Syntax error\n";
    print "Provide a directory to process, optionally with '/D' argument...\n";
}

Credits

Ofcourse almost all credit goes to the original author of this script. I just adjusted a few commands to make it work on Windows systems.

Personal tools
General
About This Wiki