Page 1 of 1

Making thumbnails with full directory structure [SOLVED]

Posted: 2014-05-29T13:08:18-07:00
by nicke
Hi,

I'm new to ImageMagic and I'm sure that someone has already asked this question but I have not been able to find the answer. So if it has been asked could someone point me in the right direction?

Ok, to the problem. I have 70 000+ photos (.jpg, CR2, CRW etc) in 100+ folder (that might have subfolders) and I want to make a small thumbnail of all of them to another directory with the "full" path. I know it is not directly a ImageMagick problem but more of a linux command line issue.

As an example, my photo structure could look like this:

/media/photos/
/media/photos/folder1/photo1.jpg
..
/media/photos/folder1/photo1450.jpg
/media/photos/folder2/folder3/photo1.CR2
..
/media/photos/folder2/folder3/photo100.CR2

and so on

I now want to have my thumbnails in another directory (for example /var/www/thumbnails/ ) with the full (from the photos directory) path like this:

/var/www/thumbnails/folder1/photo1.jpg ...
/var/www/thumbnails/folder2/folder3/photo1.jpg

With the command below (executed in /media/photos/) I get all the thumbnails in the right base directory but images in subfolders gives an error (because the subfolder does not exist)

Code: Select all

find * -name '*.jpg' -o -name "*.CR2" -o -name "*.CRW -exec convert '{}' -auto-orient -resize 640x480 -quality 60 /var/www/thumbnails/'{}'.jpg \;
I do not know if ImageMagick can write to subfolders that does not exists, so I need to figure out if it exists first and if needed create it before writing the thumbnails? Any ideas on how to solve this?

Thanks in advance!

Re: Making thumbnails with full directory structure

Posted: 2014-05-29T14:23:25-07:00
by fmw42
Imagemagick cannot create directories to my knowledge. That is a shell function.

Re: Making thumbnails with full directory structure

Posted: 2014-05-30T01:10:11-07:00
by nicke
I think I solved my problem (my tests with multiple subfolders, files with spaces etc worked well).

This script makes jpg thumbnails of all .jpg, .cr2 and .crw files (in the same folder and below its run, -iname also checks for the same extension is uppercase) if they do not exists in the destination folder and if the existsing thumbnails are older than the source file. It also preserves the subfolder structure so if you have subfolders they will also be created in the destination.

Code: Select all

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

basefolder="/home/testuser/thumbnails/"

for i in $(find * -iname '*.JPG' -o -iname "*.CR2" -o -iname "*.CRW");
do
        if [ `dirname $i` != "." ]
        then
                dirpath="${i%/*}"
                dir_arr=(`echo $dirpath | tr "/" "\n"`)
                path=""
                for x in "${dir_arr[@]}"
                do
                        if [ -z "$path" ]
                        then
                                path=$x
                                mkdir -p $basefolder$path
                        else
                                path=$path"/"$x
                                mkdir -p $basefolder$path
                        fi
                done
                ext="."${i##*.}
                output=${i/$ext/".jpg"}
                if [ ! -f $basefolder$output ] || [ $i -nt $basefolder$output ]
                then
                        echo $i
                        convert $i -auto-orient -resize 640x480 -quality 60 $basefolder$output
                fi
        else
                ext="."${i##*.}
                output=${i/$ext/".jpg"}
                if [ ! -f $basefolder$output ] || [ $i -nt $basefolder$output ]
                then
                        echo $i
                        convert $i -auto-orient -resize 640x480 -quality 60 $basefolder$output
                fi
        fi
done