PDA

View Full Version : I am in need of a simple little custom script :)



Gayowulf
June 28th, 2002, 03:37
I am totally useless at scripting of any sort. That's why I need help. I think what I need is fairly simple, though I may be wrong.

The scenario: I have several images that I need to create thumbs for. I have downloaded and compiled the latest version of NetPBM. This will do the job for me.

The problem: If I have 200 images i need thumbs for, I dont want to have to sit here typing in the command for each and every one.

Here's the basics



Assume the image is called "test.jpg", size of it is 1024x768. I
want
a thumnail of 100x100 at max, but leave the aspect ratio.

# jpegtopnm test.jpg > test.pnm

This extract the JPEG to the (non-lossy) PNM format

# pnmscale -xysize 100 100 test.pnm > test_thumb.pnm

-xysize specifies a bounding box. pnmscale scales the input image to
the
largest size that fits within the box, while preserving its aspect
ratio.

# pnmtojpeg --quality 75 test_thumb.pnm > test_thumb.jpg

Converts the pnm to a jpeg, using the jpeg lossy quality of 75%.


So what I need is some kind of script I can run that will do that for me. I specify the directory with the images it, and the word (say tn_) preceeding the thumb.

Thanks for any help- I hope its possible :)

biggulp
June 28th, 2002, 06:25
I can deal with png but not pnm

Gayowulf
June 28th, 2002, 13:33
It doesn't really matter what image types you can deal with; That has no bearing on how the script is written.

I just need something to run those few commands automatically

ansa
June 28th, 2002, 14:20
so basically you just need a shell script of something like this

#!/bin/sh

foreach f `ls`
do
echo "Converting $f from jpg to pnm...."
jpegtopnm $f.jpg > $f.pnm
echo "Scaling $f.pnm"
pnmscale -xysize 100 100 $f.pnm > $f_thumb.pnm
echo "Converting $f from pnm to jpg"
pnmtojpeg --quality 75 $f_thumb.pnm > $f_thumb.jpg
echo "Done $f"
done

# end of script

that should get you going :-)

just cd to the dir with your jpgs, and away you go.
of course i take no responsibility for this! :-)
so make sure you make a copy of your jpgs before you try it

Andy.

Gayowulf
June 28th, 2002, 16:46
Thanks, ansa.

How should I run this? I get a syntax error ( 4: Syntax error: "do" unexpected)

ansa
June 29th, 2002, 04:02
Just save it as a file, with execute permission and run it in the directory where your jpg's are.

As i hadn't etsted it i apologise, but it should be

for f in `ls`

not

foreach 4 `ls`

so change that line and it should work, but if not let me know.

Gayowulf
July 2nd, 2002, 00:44
It works, but not properly.

When I excecute the commands by hand its fine, but when run by the script the program gives me errors.

I think it has something to do with $f. is f the filename and extension or just the filename?


does for f in `ls` assign the result of ls to f?


Would there be a way to have the script go through the directory recursively and do the 3 commands on each file, then move the produced thumb to a separate dir.

Is this asking too much?

Thanks

ansa
July 2nd, 2002, 09:02
what errors does it give you?

works ok on my machine :-(

what happens is that each line in the output of ls is assigned to f, so basically each file name in turn is put into f.
the the commands are exceuted each time through the loop.
the $f just means get me the value of f

you can get the loop to do what ever you want :-)
just add a mv command to the bottom of the loop so it's like

......
echo "Converting $f from pnm to jpg"
pnmtojpeg --quality 75 $f_thumb.pnm > $f_thumb.jpg
echo "Moving $f to mydirectory..."
mv $f_thumb.jpg mydirectory
echo "Done $f"
done


the only problem i can think of is you might be using a C shell, but that's why i put the #!/bin/sh at the start which tells the shell to use this program to run the script, so there really should be no reason it doesn't work unless the original files aren't there.

let me have the errors and i should be able to work it out from that.

cheers,
Andy.

Gayowulf
July 2nd, 2002, 20:05
The script itself works fine. The errors I am getting are from the NetPBM binaries (jpegtopnm, pnmscale etc)

I don't get errors when I excecute the binaries by hand. I'm not really sure

ansa
July 3rd, 2002, 02:17
that's very bizarre!

can you let me have a copy of the errors anyway?

dmbtech
July 3rd, 2002, 15:16
I would recomend making a bash script.

GregT
July 3rd, 2002, 19:45
i can make a bash script for dat easily. i might b even able to make it a perl script. :)

Gayowulf
July 3rd, 2002, 19:59
Ok, I managed to get around the errors. The problem now is this:



for f in `ls`
do
echo "Converting $f from jpg to pnm...."
jpegtopnm $f.jpg > $f.pnm


It works fine, but the problem is that instead of filex.jpg being converted into filex.pnm, I get filex.jpg converted to filex.jpg.pnm.

I tried jpegtopnm $f > $f and jpegtopnm $f > $f.pnm but the first one does nothing, and the second has the same outcome as the first.

I'm not sure how to get around this. I know this is happening because f is the filename and extension.

ansa
July 4th, 2002, 01:59
doh! sorry about that!

ok after the `do` put this line

f=`echo $f | sed s/\.jpg//`

this strips off the trailing .jpg from the filename

again sorry i didn't spot that before


Andy.

Gayowulf
July 4th, 2002, 19:42
Thanks.

I managed to come up with


ls -1 | while read file; do
mv $file `basename $file | sed 's/.jpg//'`
done


That fixed it afterwards. It's much easier now, though:)

thanks again

ansa
July 5th, 2002, 01:55
no problem :-)