Jai’s Weblog – Tech, Security & Fun…

Tech, Security & Fun…

  • Jaibeer Malik

    Jaibeer Malik
  • View Jaibeer Malik's profile on LinkedIn
  • Subscribe

  • Feedburner

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 28 other followers

  • Archives

  • Categories

  • Stats

    • 276,101
  • Live Traffic

Image Manipulation: Using GIMP from java

Posted by Jai on October 23, 2012

Gimp is widely used image manipulation program. The in built features and plugins provide end users quite some nice functionality. The console batch mode allows users to use these plugins from command line. We will explore this feature further and we will see how we can use gimp in console batch mode directly from java for image manipulation operations.

This post is part of different image manipulation posts series posted earlier.

Calling command line utility from java

To have better understanding of image manipulation approach, please read “Image Manipulation: Using ImageMagick from java” earlier post.

You can use the ImageManipulationInfo/ImageManipulation data model to store imagemagick or gimp system command related command line options settings and that will be called on actual operating system.

Running gimp script in batch mode

Earlier post Gimp: Red Eye Effect removal using gimp script explains how you can run a gimp script in batch mode.

For example, the red eye removal custom script mentioned in the post is run using gimp batch mode from console.

Calling gimp standard batch command from java

Once we have the above two pre-requisites met for calling command utility from java and be able to run gimp script in batch mode, we are ready to integrate the gimp script to be called from java.

Let us take an example, of red eye removal script covered in earlier posts also.

./gimp-console --new-instance --console-messages --no-interface --no-data --no-fonts -b '(RedEyeRemovalGimpPlugin "/tmp/test-red-eye.jpg" "/tmp/test-red-eye-removed.jpg" 30)' -b '(gimp-quit 0)'

If we break the above final command into,

COMMAND=./gimp-console-2.6.exe --new-instance --console-messages --no-interface --no-data --no-fonts -b "COMMAND_PARAM" -b "(gimp-quit 0)"
COMMAND_PARAM=(RedEyeRemovalGimpPlugin INPUT_FILE OUTPUT_FILE THRESHOLD_VALUE)

Now for the red eye removal script the parameters are input file, output file and the threshold value. Use key-value pair for different parameters and you can generate final command_param and accordingly the command string to be executed. The parameters INPUT_FILE (eg. “/tmp/test-red-eye.jpg”) and OUTPUT_FILE (eg. “/tmp/test-red-eye-removed.jpg”) are files absolute paths on the system. The threshold for the red eye removal THRESHOLD_VALUE (from 0 to 100, eg. 30) can be user selection based on a slider.

You can execute any gimp custom script now from java, it depends on the complexity of the gimp script.

Calling gimp wrapper script from java

The big challenge as usual is,

  • How to handle complex gimp scripts
  • How to handle overhead of starting new gimp instance everytime
  • How to parse special characters in gimp script
  • How to handle string quotes issues between unix/windows environments.

The simple approach to avoid above problem is to have custom bat/shell script to take input from java and parse the input parameters of script and accordingly execute the gimp commands to single gimp instance.

To understand the gimp wrapper approach, let us use our earlier mentioned approach of image manipulation from java,

GIMP_COMMAND=./gimp-wrapper-script.sh

ImageManipulation

ImageManipulation imageManipulation = new ImageManipulation();
//${PARAM}, we can use template to replace parameters in a string.
imageManipulation.setCommandLineOptions("(RedEyeRemovalGimpPlugin ${INPUT_FILE} ${OUTPUT_FILE} ${THRESHOLD_VALUE})");
//can be system property, different for unix/windows. It is path to executable script.
imageManipulation.setCommandType("GIMP_COMMAND");
//Key-value pair to be relaced in commandLineOptions above.
//The map is generated based on your runtime params values
//eg. inputfile, outputfile, and threshold in case of red eye removal: Map<String, String> params
imageManipulation.setParams(params);
....

}

The final command executed on the system will be,

./gimp-wrapper-script.sh '(RedEyeRemovalGimpPlugin "/tmp/test-red-eye.jpg" "/tmp/test-red-eye-removed.jpg" 30)'

Now there are two ways to execute the gimp console command from here on,

Running Gimp new instance for every call

Parse parameters in simple bash script,

#!/bin/bash
fullCommand=$*
len=${#fullCommand}
reqlen=`expr $len - 2`
trimmedfullcommand=${fullCommand:1:$reqlen}

#Trimming is required to solve string quote issues around positional params and file names.

`gimp-console --new-instance --console-messages --no-interface --no-data --no-fonts -b "$trimmedfullcommand" -b '(gimp-quit 0)'`

Do the same with simple perl script,

#!/usr/bin/perl

my $commandline = join(" ", @ARGV );
`gimp-console --new-instance --console-messages --no-interface --no-data --no-fonts -b '$commandline' -b '(gimp-quit 0)'`

Running Gimp console in daemon mode

To solve the performance issues, we need to start the gimp server as daemon in batch mode,

gimp --new-instance --no-interface --no-data --no-fonts -b -

Start socket connection at local port, to which this gimp console will listen to. And user will push the input to network socket.

my $sock = new IO::Socket::INET( LocalHost => "127.0.0.1",
				LocalPort => "12345",
				Proto => 'tcp',
				Listen => 1,
				Reuse => 0, );

User input to network socket, meaning the gimp wrapper script will look like

#!/bin/bash
echo "$*" | nc 127.0.0.1 12345

The processing time difference is huge in both the approach. The above design model will allow you to execute any gimp script in performing way from java.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 28 other followers

%d bloggers like this: