Saturday, November 24, 2007

Automatic Picture Manipulation with The GIMP

We like sepia-tone pictures, especially for romantic stuff like weddings. In case you ever have a thousand or even ten pictures you'd like to see in sepia, try this script for The GIMP instead of pointing and clicking:

(define (batch-old-photo pattern)
(let* ((filelist (cadr (file-glob pattern 1))))
(while (not (null? filelist))
(let* ((filename (car filelist))
(image (car (gimp-file-load RUN-NONINTERACTIVE
filename filename)))
(drawable (car (gimp-image-get-active-layer image))))
(script-fu-old-photo
image
drawable
TRUE
0
TRUE
FALSE
FALSE)
(set! drawable (car (gimp-image-get-active-layer image)))
(gimp-file-save RUN-NONINTERACTIVE
image drawable filename filename)
(gimp-image-delete image))
(set! filelist (cdr filelist)))))

You can use this script by:
  1. save everything between the parentheses above in a file named ~/.gimp-2.2/scripts/batch-old-photo.scm
  2. copy the files you'd like to convert to sepia to a new directory. WARNING: this script modifies the files it's reading, so make sure you've made copies or your originals will be gone
  3. change into that directory and execute:
gimp -i -b '(batch-old-photo "*.jpg")' -b '(gimp-quit 0)'

For more on the basics of scripting The GIMP, see their tutorial.

Enjoy your new old-timey photos.

Tuesday, November 20, 2007

Care to search for integers that sum?

I spent the better part of yesterday pondering how to efficiently search a sorted array of integers to see if a pair of integers within the array sum to given integer.

i.e., given the numbers 1, 2, 3, 5, 8, 13 -- does any pair of them add up to 8 (yes: 3+5), 13 (yes: 5+8), or 17 (no)?

There's several ways to solve this problem, but the trick is to do it in linear time (only visit each number in the array once) and linear space (don't make any copies of the numbers in the array).

After giving it the old college try, I searched for the proper algorithm and found it.

Here's the resulting Java:

public class IntsForSumJava {

public static boolean doIntsForSumExist(int[] inputs, int target_sum) {
int start_idx = 0;
int end_idx = inputs.length - 1;
boolean found_target = false;
int iterations = 0;

while (!found_target) {
iterations++;
int start = inputs[start_idx];
int end = inputs[end_idx];
if (end == (target_sum - start)) {
found_target = true;
} else if (end > (target_sum - start)) {
end_idx--;
if (end_idx > end_idx) {
break;
}
}
}
System.out.println("iterations: " + iterations);
return found_target;
}

public static void main(String[] args) {
int[] inputs = {1, 2, 3, 5, 8, 13};
int target_sum = 0;
System.out.println("found target (" + target_sum + "): " +
IntsForSumJava.doIntsForSumExist(inputs, target_sum));
target_sum = 8;
System.out.println("found target (" + target_sum + "): " +
IntsForSumJava.doIntsForSumExist(inputs, target_sum));
target_sum = 13;
System.out.println("found target (" + target_sum + "): " +
IntsForSumJava.doIntsForSumExist(inputs, target_sum));
target_sum = 17;
System.out.println("found target (" + target_sum + "): " +
IntsForSumJava.doIntsForSumExist(inputs, target_sum));
}
}

Just in case you were wondering...

Happy searching.

Sunday, November 18, 2007

Just Married!

We did it. Jen and I tied the knot November 3 in Maui.


The ceremony and reception went great -- even capped off with a fantastic fireworks display (courtesy of some pharma company). Thanks so much to everyone who attended or wished us well.

Oh, we took some pictures.

I'll have more later on our activities in Maui, Kauai, and Oahu.