Web Hosting Talk







View Full Version : Command line batch processing...


acctman
04-08-2005, 01:42 AM
hi i'm using the command line below to process one image at a time manually. how would i make it process an entire directory or .jpg's

composite -gravity center /home/missbot/public_html/images/watermark.png original_img.jpg processed_img.jpg

hozt
04-08-2005, 02:13 AM
You should be able to write a shell script to do this. I do something similar to create thumnails. To run the script: sh name_of_script *.jpg

#!/bin/sh
for file in "$@"
do
composite -gravity center /home/missbot/public_html/images/watermark.png "$file"
done

innova
04-11-2005, 04:40 AM
Your example makes no sense.

The '$@' refers to the positional parameters passed to a script (or to a function, actually), it does not in any way signify 'all files' as you think it would.

What you meant to do was the following:

for file in *

The * is expanded by the bash shell (with default globbing rules) to all files in the current folder... except files that start with a period like .bash_history and such.

In examples I commonly see people do stuff like this:

for file in `ls *`

This makes no sense really, if you think about what '*' means to the shell, it is redundant and launches an unnecessary external program.

Anyway, shell scripting rules!

artofmobile
04-11-2005, 05:33 AM
No command line, how's about perl script:

#!/usr/local/bin/perl

my $dir='/directory';
my $ext='.jpg';
my $added="-new";
my @all=`ls $dir/*$ext`;

foreach (@all) {
chomp;
my $new=$_;
$new=~s/$ext$//g;
`composite -gravity center /home/missbot/public_html/images/watermark.png $_ $new$added$ext`;
}

acctman
04-11-2005, 12:21 PM
thanks for the perl script example, i havent tried it yet but i see one possible error... the basic setup for using the composite command is

composite -gravity center /home/missbot/public_html/images/watermark.png INPUT_NAME.jpg OUTPUT_NAME.jpg

there needs to be an output_name.jpg in which I usually put the same name and overwrite

artofmobile
04-11-2005, 06:49 PM
Originally posted by acctman
thanks for the perl script example, i havent tried it yet but i see one possible error... the basic setup for using the composite command is

composite -gravity center /home/missbot/public_html/images/watermark.png INPUT_NAME.jpg OUTPUT_NAME.jpg

there needs to be an output_name.jpg in which I usually put the same name and overwrite

Possible Error?? Huh, I don't seem to see " output_name.jpg in which I usually put the same name and overwrite" as your requirement in your first post. ;)

By the way, the possible Error is there shouldn't be something *-new.jpg in the directory before hand.

Anyway, if that's your requirement, then just modify the perl script to suit your requirement.

acctman
04-11-2005, 07:05 PM
Originally posted by artofmobile
Possible Error?? Huh, I don't seem to see " output_name.jpg in which I usually put the same name and overwrite" as your requirement in your first post. ;)

By the way, the possible Error is there shouldn't be something *-new.jpg in the directory before hand.

Anyway, if that's your requirement, then just modify the perl script to suit your requirement.

you're good =) code worked. thanks :banana:

error404
04-11-2005, 07:14 PM
Originally posted by innova
Your example makes no sense.

The '$@' refers to the positional parameters passed to a script (or to a function, actually), it does not in any way signify 'all files' as you think it would.

If he typed script.sh *.jpg, the shell would expand the glob and pass each file as it's own parameter to the script. That way the glob isn't hardcoded and any pattern can be easily used without editing the script.

His script should work fine, as far as I can see.

acctman
04-12-2005, 06:28 PM
Originally posted by artofmobile
No command line, how's about perl script:

#!/usr/local/bin/perl

my $dir='/directory';
my $ext='.jpg';
my $added="-new";
my @all=`ls $dir/*$ext`;

foreach (@all) {
chomp;
my $new=$_;
$new=~s/$ext$//g;
`composite -gravity center /home/missbot/public_html/images/watermark.png $_ $new$added$ext`;
}

this doesn't work on a list of 4000+ images
sh: line 1: /bin/ls: Argument list too long

acctman
04-12-2005, 06:57 PM
nevermind i used the first coding on to /bin/sh one