簡體   English   中英

Bin Bash Terminal腳本使用Mac SIPS調整PNG / CR2 / JPG圖像的大小並節省空間

[英]Bin Bash Terminal script to resize PNG/CR2/JPG images using Mac SIPS with space saving check

我編寫了一個很棒的小腳本,將我的JPG / PNG / CR2(原始文件)的大量照片集轉換為2880px 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

這是完整的腳本! :)

##################################
#!/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!

當然。 在頂部的腳本中設置一個變量,或者傳入一個新參數,該參數說明要在何處創建新的樹結構。 或混合使用,並設置默認的新根目錄,但允許用戶在命令行上使用第二個參數覆蓋它,如下所示:

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM