Making thumbnails with full directory structure [SOLVED]

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
nicke
Posts: 2
Joined: 2014-05-29T12:30:52-07:00
Authentication code: 6789

Making thumbnails with full directory structure [SOLVED]

Post 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!
Last edited by nicke on 2014-05-30T01:11:03-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Making thumbnails with full directory structure

Post by fmw42 »

Imagemagick cannot create directories to my knowledge. That is a shell function.
nicke
Posts: 2
Joined: 2014-05-29T12:30:52-07:00
Authentication code: 6789

Re: Making thumbnails with full directory structure

Post 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
Post Reply