Bin Bash终端脚本,使用Mac SIPS调整PNG / CR2 / JPG图像的大小并节省空间

时间:2014-10-27 09:33:11

标签: macos bash terminal image-resizing sips

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

这使我可以将我的所有照片放在我的电脑上,同时将巨型原件存放在外部硬盘上。最好的照片看起来很棒,因为它们的重新调整到了我2880px宽的精确物理屏幕分辨率!

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


好的,我的问题......

我的外置硬盘上的图片存储方式如下:

  

(硬盘根目录)

     
    

图片

         
      

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

2 个答案:

答案 0 :(得分: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

感谢Mark Setchell的帮助!

答案 1 :(得分: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"