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

查看:110
本文介绍了Bin Bash Terminal脚本使用Mac SIPS调整PNG/CR2/JPG图像的大小并节省空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个很棒的小脚本,将我的JPG/PNG/CR2(原始文件)的大量照片集转换为2880px JPEGs的MacBook Pro Retina分辨率.

I made a cool little script to take my massive photo collection of JPG/PNG/CR2 (raw files) and convert them into MacBook Pro Retina resolution of 2880px JPEGs.

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

This allows me to have all my pics on my computer while storing the giant originals on an external hard drive. Best of all the pics look fantastic because they're resized to my exact physical screen resolution 2880px wide!

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

You can easily adapt the script to your own needs...

好吧,我的问题....

OK so on to my question....

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

My pictures on my external hard drive are stored like this:

(硬盘根)

(Hard drive root)

图片

2013-05佛蒙特州婚礼

2013-05 Wedding in Vermont

img_0001.jpg

img_0001.jpg

img_0002.jpg

img_0002.jpg

2014-10拉斯维加斯之行

2014-10 Las Vegas Trip

img_0001.cr2

img_0001.cr2

img_0002.cr2

img_0002.cr2

...

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

Right now the script OVERWRITES the original files... so I always need to make a complete copy of all my pictures to a 2nd drive and then run the script. Is there an easy way to make the script re-create the entire directory structure and write the new files out to a new folder/drive while maintaining the directory structure?

感谢您的帮助!

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

推荐答案

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

Sure. Either set a variable in the script at the top, or pass in a new parameter that says where the new tree structure is to be created. Or do a mixture, and set a default new root directory but allow the user to overwrite it with a second parameter on the command line like this:

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

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

Then use dirname to get the name of the directory that each input image is in, like this:

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

那会给你

/users/bozo/tmp

现在将新目录路径放在最前面

now put the new directory path on the front

newpath="$newroot/$dir"

并使其成功,包括所有介入的文件夹,并忽略错误

and make it, including all intervening folders and ignoring errors with

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

并更改您的命令以这样输出

and change your commands to output like this

 file=$(basename "input image path")
 sips ... --out "$newpath/$file"

这篇关于Bin Bash Terminal脚本使用Mac SIPS调整PNG/CR2/JPG图像的大小并节省空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆