E.A.T
E.A.T

Reputation: 858

imagemagik convert files by directory

I wrote this simple shell script to convert jpgs using imagemagik. It works fine, but I would like to include pngs, gifs, jpeg, etc... while passing the file extension through the script for each iteration of the find. I do prefer this approach of looping over a find, so that I can better report on each item processed, and allow a more scalable script for adding other sizes and transformations to each process. (rather than a simple convert * command.).

Any suggestions?

find cdn/ -name '*.jpg' -print |  sort | 
while read f; 
 do 
 b=$(basename $f .jpg)
 in="${b}.jpg"
 thumb="${b}_150x150.jpg" 
if [ -e $thumb ]; 
then 
 true  
else 
 convert -resize 150 $in $thumb   
fi

done

Upvotes: 1

Views: 181

Answers (2)

xpt
xpt

Reputation: 22994

+1 for "Splitting up the problem into 1) finding the files, 2) deciding what to do with a file and 3) process the file. Making it modular will split the problem into parts which you can tackle separately." as previously suggested. This allow a more scalable approach for adding more processings.

This way, you don't need to pass the files by extensions. Is that what you want, passing the files by extensions? Do you have to do that?

Also,

  • Use -iname '*.jpg' instead of -name '*.jpg' so as to do a case-insensitive search.
  • Use more -iname parameters on the find to find all other extensions that you want. E.g.,

    find cdn/ \(-iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' \) -print

I.e., you find all the files you want in one-pass, instead of using find over and over to find different extension files.

Upvotes: 1

user2152447
user2152447

Reputation: 49

Make it more modular, call a second script to process the image files.

find /path -type f -print |
while read filename ; do
     sh /path/to/process_image $filename
done

Within the process_image script you can then choose what do do based on the file's extension name or file type. The script could call other scripts depending on what you want to do based on the image type, size etc.

Splitting up the problem into 1) finding the files, 2) deciding what to do with a file and 3) process the file. Making it modular will split the problem into parts which you can tackle separately.

Upvotes: 1

Related Questions