views:

19

answers:

1

Question: How do I recursively process, using Imagemagik (convert), a nested directory of photos?

I have the following directory structure:

/
..2008/
....a.jpg
....b.jpg
..2009/
.....c.jpg

And I want to run the following ImageMagik command on each file, to clean/resize up the images, and then save the resulting image out as the exact same filename as the original file. Basically, I want to replace the original file with the generated resized file created.

// from unix command line    
convert FILENAME.jpg -resize 100x100 -sharpen 1.5 -strip -profile "*" -sampling-factor 4x1 -quality 80 FILENAME.jpg;
+3  A: 

Try using find -exec. For instance:

find dirname -type f -iname "*.jpg" -exec convert \{\} -resize 100x100 -sharpen 1.5 -strip -profile "*" -sampling-factor 4x1 -quality 80 \{\} \;

By the way, I don't recommend in-place editing. It's generally a bad idea, especially with storage so cheap. Why not be safe?

Borealid
at what directory level would I run that from? /? /2008/ ?
TeddyR
and what is 'dirname'?
TeddyR
When I run the command above, I get the following error: "missing argument to `-exec'".
TeddyR
Do I need to quote the "convert ..." part of your statement?
TeddyR
If you want, replace 'dirname' with '.' and run it from the top-level directory. It will recurse down for you. Just run the find without the '-exec' and trailers to see on which files the command will be run.
Borealid
Oh, sorry, extraneous semicolon - fixed.
Borealid
@Borealid, when I run 'find . -type f -iname "*.jpg"' - it appears to list every file I want to process and resize. So now, how do I use "find dirname -type f -iname "*.jpg"" in conjection with my "convert" script?
TeddyR
@TeddyR : you just append "-exec", then the command you want to run with every instance of the filename replaced with "\{\}", then "\;" and it will work.
Borealid
Well, I lie - but it will work if there are no filenames containing spaces, exclamation points, or other special characters. Otherwise, quote the name.
Borealid
Error: "convert: unable to open file `*': No such file or directory." when I run the following from the parent directory. 'find . -type f -iname "*.jpg" -exec convert \{\} -resize 100x100 -sharpen 1.5 -strip -profile "*" -sampling-factor 4x1 -quality 80 \{\} \;'
TeddyR
My filenames are only digits (e.g. 12323.jpg) and my directories are named with only digits as well (e.g. /11/, /22/ )
TeddyR
That looks like your convert "-profile" command not working. Find is running it OK there.
Borealid