Bash for image resize using ImageMagick

Hi all,

I have a FreeNAS server hosting all of my images. The original images are high resolution. What I would like to do is 2 parts:

  1. do a batch resize of all of the images so I have web-friendly version and print ready version
  2. run a cron job to apply the bash to any "new" files

First I want to tackle the bash script. As far as I can tell, these are the things that I must take into account:

  1. I have a directory structure that I want to duplicate and maintain
  2. I want to keep the originals
  3. I want a duplicate that is web-friendly
  4. Original dir is "./pictures"
  5. New dir is "./resized"
  6. Script will live in the "/pictures" dir
  7. Identify new images

I think that is it! :rolleyes:

I know how to copy the structure but I don't think that Imagemagick will check recursively by itself. That is where I am stuck.

I'm relatively new to bash, but here is what I have so far:

#!/bin/bash

# find all directories, copy structure

find . -type d | cpio -pvdm ../resized

# creates the new image, move to new dir

for f in *.jpg;
do
    echo "Processing $f"
    convert -resize "50%"  \
        $f ./resized/$f
done

The copy of the image "$f ./resized/$f" will just be dumped into the root of "/resized". So I am stuck there too.

I'm also wondering about the logic for the next step of checking for new images, then running the convert part on that. Is there a command that will compare the dir and identify the new files?

I appreciate any help!

Thanks!
:slight_smile:

This code copy in Dir2 the files of Dir1 if the file don't exist en Dir2

Dir1=$1
Dir2=$2
for file in `find $Dir1  -type f `
do
	name=`basename $file`
	if [ ! -f "$Dir2/$name" ]
	then
	  cp $file $Dir2
	fi
done