0

我制作了一个很酷的小脚本来拍摄我的大量 JPG/PNG/CR2(原始文件)照片集,并将它们转换为 2880 像素 JPEG 的 MacBook Pro Retina 分辨率。

这使我可以将所有照片保存在计算机上,同时将巨大的原件存储在外部硬盘驱动器上。最好的照片看起来很棒,因为它们已调整到我的确切物理屏幕分辨率 2880 像素宽!

您可以轻松地根据自己的需要调整脚本...


好的等等我的问题....

我的外部硬盘驱动器上的图片存储如下:

(硬盘根目录)

图片

2013-05 佛蒙特州婚礼

img_0001.jpg

img_0002.jpg

2014-10 拉斯维加斯之旅

img_0001.cr2

img_0002.cr2

...

现在脚本覆盖了原始文件......所以我总是需要将所有图片的完整副本复制到第二个驱动器,然后运行脚本。是否有一种简单的方法可以使脚本重新创建整个目录结构并将新文件写入新文件夹/驱动器,同时保持目录结构?

感谢您的帮助!

##################################
#!/bin/bash - resize2880px.sh (It's for Mac OS X computers)
# By Jason Fox of GetFoxy.com 2014 - hit me at jfox {at} foxnv.com if you have questions
# This script converts all PNG/JPG/CR2 files to JPG at a max resolution of 2880px (saving tons of space in the process).
# run it like this:
# 0. Save this script in your Documents Folder as resize2880px.sh
# 1. Open Terminal and CD into the directory of pictures to shrink
# 2. paste in:  find . -type f \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.cr2" \) -exec sh ~/Documents/resize2880px.sh {} \;
# 3. If you have the awesome JPEGMini app... use it now to further save space! ;)

#the sizes to convert to
width=2880                                                                              
height=2880

#theFile given in input   
theFile=$1
echo ""
echo "$theFile"

#using sips to retrieve the width & height            
#size[0] = width
#size[1] = height
size=($(sips -g pixelWidth -g pixelHeight "$theFile" | grep -o '[0-9]*$'))                     

if [[ ${size[0]} -le $width && ${size[1]} -le $height ]];
    then echo "Width = ${size[0]} & Height = ${size[1]} SO... W<=$width H<=$height - no resize - just JPG convert"
    sips -s format jpeg "$theFile" --out "$theFile-t2880px.jpg"
elif [[ ${size[0]} -gt $width && ${size[1]} -le $height ]];
    then echo "Width = ${size[0]} & Height = ${size[1]} SO... W>$width H<=$height - Run SIPS $width JPG conversion"
    sips -Z 2880 -s format jpeg "$theFile" --out "$theFile-t2880px.jpg"
elif [[ ${size[0]} -le $width && ${size[1]} -gt $height ]];
    then echo "Width = ${size[0]} & Height = ${size[1]} SO... W<=$width H>$height - Run SIPS $width JPG conversion"
    sips -Z 2880 -s format jpeg "$theFile" --out "$theFile-t2880px.jpg"
elif [[ ${size[0]} -gt $width && ${size[1]} -gt $height ]];
    then echo "Width = ${size[0]} & Height = ${size[1]} SO... W>$width H>=$height - Run SIPS $width JPG conversion"
    sips -Z 2880 -s format jpeg "$theFile" --out "$theFile-t2880px.jpg"
else echo "Something is wrong."
fi

# Determine number of file system blocks used to store the original and new files
origfilesize=$(ls -s "$theFile" | awk '{print $1}')
newfilesize=$(ls -s "$theFile-t2880px.jpg" | awk '{print $1}')

if [[ $origfilesize -le $newfilesize ]];
    then echo "$origfilesize is less than or equal to $newfilesize - no space saved so deleting the new file"
    rm "$theFile-t2880px.jpg"
else
    echo "$origfilesize is greater than $newfilesize - deleting original file"
    rm "$theFile"   
fi
4

2 回答 2

2

这是完成的脚本!:)

##################################
#!/bin/bash - resize2880px.sh (It's for Mac OS X v10.6+ computers - Tested on v10.10 Yosemite)
# By Jason Fox of GetFoxy.com 2014 - hit me at jfox {at} foxnv.com if you have questions
# This script converts all PNG/JPG/CR2 files to JPG at a max resolution of 2880px (saving tons of space).
# run it like this:
# 0. Save this script in your Documents Folder as resize2880px.sh
# 1. Open Terminal and CD into the directory of pictures to shrink
# 2. paste in:  find . -type f \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.cr2" \) -exec sh ~/Documents/resize2880px.sh {} \;
# 3. If you have the awesome JPEGMini app... use it to further save space! ;)

#Define the output folder
outputfolder=~/Desktop/2880px

#the sizes to convert to (max pixels)
width=2880                                                                              
height=2880

theFile=$1
echo ""

dir=$(dirname "$theFile")
newpath=$outputfolder/$dir/
echo $theFile will move to $newpath
mkdir -p "$newpath" 

#using sips to retrieve the width & height            
#size[0] = width
#size[1] = height
size=($(sips -g pixelWidth -g pixelHeight "$theFile" | grep -o '[0-9]*$'))                     

if [[ ${size[0]} -le $width && ${size[1]} -le $height ]];
    then echo "Width = ${size[0]} & Height = ${size[1]} SO... W<=$width H<=$height - no resize - just JPG convert"
    sips -s format jpeg "$theFile" --out "$outputfolder/$theFile-t2880px.jpg"
elif [[ ${size[0]} -gt $width && ${size[1]} -le $height ]];
    then echo "Width = ${size[0]} & Height = ${size[1]} SO... W>$width H<=$height - Run SIPS $width JPG conversion"
    sips -Z 2880 -s format jpeg "$theFile" --out "$outputfolder/$theFile-t2880px.jpg"
elif [[ ${size[0]} -le $width && ${size[1]} -gt $height ]];
    then echo "Width = ${size[0]} & Height = ${size[1]} SO... W<=$width H>$height - Run SIPS $width JPG conversion"
    sips -Z 2880 -s format jpeg "$theFile" --out "$outputfolder/$theFile-t2880px.jpg"
elif [[ ${size[0]} -gt $width && ${size[1]} -gt $height ]];
    then echo "Width = ${size[0]} & Height = ${size[1]} SO... W>$width H>=$height - Run SIPS $width JPG conversion"
    sips -Z 2880 -s format jpeg "$theFile" --out "$outputfolder/$theFile-t2880px.jpg"
else echo "Something is wrong."
fi

# Determine number of file system blocks used to store the original and new files
origfilesize=$(ls -s "$theFile" | awk '{print $1}')
newfilesize=$(ls -s "$outputfolder/$theFile-t2880px.jpg" | awk '{print $1}')

# File Size comparison (make sure we're saving space)
if [[ $origfilesize -lt $newfilesize ]];
    then echo "$origfilesize is less than to $newfilesize - delete new file - use original file instead"
    rm "$outputfolder/$theFile-t2880px.jpg"
    cp "$theFile" "$outputfolder/$theFile"
fi

感谢马克塞切尔的帮助!

于 2014-10-27T11:56:30.230 回答
0

当然。要么在顶部的脚本中设置一个变量,要么传入一个新参数,说明要在哪里创建新的树结构。或者混合使用,并设置一个默认的新根目录,但允许用户在命令行上使用第二个参数覆盖它,如下所示:

newroot=${2:-~/Desktop/resized}

然后用于dirname获取每个输入图像所在目录的名称,如下所示:

dir=$(dirname "/users/bozo/tmp/image.jpg")

那会给你

/users/bozo/tmp

现在把新的目录路径放在前面

newpath="$newroot/$dir"

并制作它,包括所有干预文件夹并忽略错误

mkdir -p "$newpath" 2> /dev/null

并将您的命令更改为这样的输出

 file=$(basename "input image path")
 sips ... --out "$newpath/$file"
于 2014-10-27T09:52:12.610 回答