如何递归地将图像从一个文件夹调整到另一个文件夹?

How to recursively resize images from one folder to another?

我想创建我组织到一组嵌套子目录中的图像的缩略图到文件结构的镜像中,这样类型的命令:

./imageresize.sh Large Small 10

...将转换嵌套在“./Large”下的目录中的任何 .jpg 或 .JPG 文件:

./Large/Holidays/001.jpg
./Large/Holidays/002.jpg
./Large/Holidays/003.jpg
./Large/Pets/Dog/001.jpg
./Large/Pets/Dog/002.jpg
./Large/Pets/Cat/001.jpg

到具有不同顶级目录的镜像目标的 10% 的缩略图("Small" 而不是 "Large" 在这个例子中):

./Small/Holidays/001.jpg
./Small/Holidays/002.jpg
./Small/Holidays/003.jpg
./Small/Pets/Dog/001.jpg
./Small/Pets/Dog/002.jpg
./Small/Pets/Cat/001.jpg

这是我目前所拥有的,但我似乎无法让它发挥作用。 $newfile 变量好像无效但是不知道为什么,测试的时候把'convert'命令的结果输出到屏幕上。任何 help/suggestions 非常感谢。

#!/bin/bash

#manage whitespace and escape characters
OIFS="$IFS"
IFS=$'\n'

#create file list
filelist=$(find .// -name "*.jpg" -o -name "*.JPG")

for file in $filelist
do

#create destination path for 'convert' command
newfile= ${file//}

convert "$file" -define jpeg:extent=10kb -scale % "$newfile"
done

不知道这是否只是一个 copy/paste 错误,或者它是否确实在您的脚本中,但是这一行:

newfile= ${file//}

将是 Bash 中的无效赋值,因为赋值时不允许 = 周围有空格。

试试这个:

newfile=${file//}

作为旁注。 find 也有不区分大小写的搜索,-iname,所以你可以这样做:

filelist=$(find .// -iname "*.jpg")

它还有 -exec 用于在结果集上执行命令。在这里解释得很好:https://unix.stackexchange.com/questions/12902/how-to-run-find-exec 所以你可以一次完成 find。 (注:这不一定更好,大多数情况下只是偏好问题,我只是提一下这个可能性。)

因此 - 使用 madsen 和 4ae1e1(感谢两位)建议的更正修改了工作脚本,并使用 rsync 命令首先创建目录结构,(然后清除目标中的无关文件):)。我添加了一个额外的参数和参数检查器,这样您现在就可以指定源文件、目标文件、大约目标文件大小(以 kb 为单位)和原始文件的百分比。希望它能帮助别人。 :)

#!/bin/bash

#manage whitespace and escape characters
OIFS="$IFS"
IFS=$'\n'

#check parameters
if [ $# -lt 4 ] || [ "" = "--help" ]
then # if no parameters or '--help' as  - show help
    echo "______________________________________________________________"
    echo ""
    echo "Useage: thumbnailmirror [Source] [Destination] [Filesize] [Percentage]"
    echo "Source - e.g. 'Fullsize' (directory must exist)" 
    echo "Destination - e.g. 'Thumnail' (directory must exist)"
    echo "Filesize - approx filesize in kb  e.g. '10'"
    echo "Percentage - % of reduction (1-100)"
    echo "e.g. thumbnailmirror Fullsize Thumnail 18 7"
    echo "______________________________________________________________"


else # parameters exist
    #syncronise directory structure (directories only)
    rsync -a --include '*/' --exclude  '*'  .// .//
    # delete any extraneous files and directories at destination
    rsync -a --delete --existing --ignore-existing .// .//
    #create file list ( -iname means not case sensitive)
    filelist=$(find .// -iname "*.jpg")

    for file in $filelist
      do
      #define destination filename for 'convert' command
      newfile=${file//}

      if [ ! -f "$newfile" ] #if file doesn't exists create it
      then
          convert "$file" -define jpeg:extent=kb -quality 100 -scale % "$newfile"
          echo "$file resized"
      else #skip it
          echo "Skipping $file - exists already"
      fi
      done
fi