将Apple标签(Mavericks标签)导出到IPTC“关键字”元数据的Bash脚本(使用'Tag'和'ExifTool')

时间:2015-06-05 12:58:24

标签: bash tags metadata photos iptc

我的最终解决方案:

将以下脚本(从damienfrancois答案修改)保存到“photos.sh”等文件中。

IFS=$'\n';
for file in $(find ./ -name '*.jpg' -or -name '*.JPG' -or -name '*.tif' -or -name '*.JPEG'); # iterate over each file
do
  taglist="$(tag --no-name --list "$file")" # get a comma-separated list (string) of tags
  IFS=',' read -ra tagarray <<< "$taglist" # convert that string to an array
  for tag in "${tagarray[@]}" # loop over that array of tags
  do
    exiftool -Keywords+="$tag" "$file" # add tag to file
  done
done

不要忘记通过执行以下操作使脚本可执行

chmod 755 /path/to/script/dir/photos.sh

安装“由JDBerry标记”并安装“Phil Harvey的ExifTool”。使用终端转到所选目录。此目录中只能包含“.jpg”,“.JPG”,“。tt”和“.JPEG”文件,脚本将以递归方式遍历根目录,但不会更改其他文件类型。成功的输出应该如下所示:

~ > cd /path/to/images/dir/
/path/to/images/dir/ > /path/to/script/dir/photos.sh
    1 image files updated
    1 image files updated

该脚本会将原始文件的副本保留为“img.jpg_original”。所有Apple标签都将从最终文件“img.jpg”中删除。在确定一切正常后(我使用Spotlight),请记得删除“_original”文件。

我原来的问题:

我经常在OS X上使用终端来执行rysnc,ssh等任务,但在bash脚本中仍然是一个完整的菜鸟。客户端拥有大量使用OS X标记标记的图像。我需要将这些标签附加到IPTC元数据中。

到目前为止,我已经能够使用“由JDBerry标记”

来执行以下操作
~ > tag --no-name --list /path/to/img/example.jpg 
    Orange,Red

我也可以使用Phil Harvey的ExifTool进行以下操作

~ > exiftool -Keywords+='Orange' /path/to/img/example.jpg
    1 image files updated
~ > exiftool -Keywords+='Red' /path/to/img/example.jpg
    1 image files updated

是否有任何Bash Scripting大师愿意帮助我?我正在考虑以下内容(用伪代码编写):

$imgDir[] = function that adds all images in directory to array;
foreach ($imgDir as $pathToImg) {
    $tagsArray[] = function that executes "tag --no-name --list $pathToImg" and saves return value;
    $numberOfTags = count($tagsArray);
    if ($numberOfTags != NULL) {
        for ($i = 1; $i <= $numberOfTags; $i++) {
            function that executes "exiftool -Keywords+='$tagsArray[$i-1]' $pathToImg;"
        } 
    }
}

3 个答案:

答案 0 :(得分:2)

这是一个适用于您的未经测试的解决方案。它可能需要抛光。

for file in /path/to/img/*.jpg # iterate over each file
do
  taglist="$(tag --no-name --list \"$file\")" # get a comma-separated list (string) of tags
  IFS=',' read -ra tagarray <<< "$taglist" # convert that string to an array
  for tag in "${tagarray[@]}" # loop over that array of tags
  do
    exiftool -Keywords+="$tag" "$file" # add tag to file
  end
end

您可以将内部for循环和read命令合并到一个while循环中,但我认为这会牺牲可读性。

答案 1 :(得分:2)

Automator工作流程

您好,我尝试用它来制作 automator工作流程

注意:

默认情况下,Apple在Mavericks中启用了智能引号 这意味着默认情况下会自动更正并崩溃您的代码

在Automator中,您必须在“运行shell脚本”文本框中右键单击

/上下文菜单/替换选项/智能引号
并使用每个新文档/副本禁用它。

*更新:

您还可以通过系统偏好设置/键盘/文字/智能报价中的将自动更正设置为“直线报价”来禁用它(下拉列表中的最低项目)之后可选择禁用自动更正。 这将永久更改automator中的默认值。

https://derflounder.wordpress.com/2014/02/01/disabling-smart-quotes-in-mavericks/

首先我做了一个“询问取景器项目”框:

键入:文件夹 允许多项选择

然后我添加了一个“运行shell脚本”框:

shell:bin / bash
传递输入:作为参数

并在选项中: 启用工作流程时显示此操作。

,这是代码的另一个迭代:

    #!/bin/bash

cd "$1" #changes directory to selected folder
IFS=$'\n';
for file in $(find ./ -name '*.jpg' -or -name '*.jpeg' -or -name '*.JPG' -or -name '*.pdf' -or -name '*.tiff'); #added all file types i liked.
do
    taglist="$(/usr/local/bin/tag --no-name --list "$file")" # inserted full file paths /usr/local/bin/tag to make automator find it. these paths can vary. 
    IFS=',' read -ra tagarray <<< "$taglist"
    for tag in "${tagarray[@]}" 
    do
        /usr/local/bin/exiftool -Keywords-="$tag" "$file" -Keywords+="$tag" "$file" -overwrite_original_in_place --a 
#inserted full file paths /usr/local/bin/exiftool to make automator find it. these paths can vary
#added -overwrite_original_in_place = updating original image:Can possible DESTROY! your data. Use with caution! 
#added -Keywords-="$tag" to prevent duplicate keywords: removes allready assigned keywords first.
        done 
done

我是新手 所以小心处理。

如果您找到更好的解决方案,请随时修复。我很乐意听到你的方法。

感谢所有之前的发言人。

答案 2 :(得分:0)

这已经在上面的答案中进行了修改

好的,这就是我为那些希望了解细节的Mac用户所做的工作。

将以下脚本(从damienfrancois答案修改)保存到“photos.sh”等文件中。

for file in *.jpg # iterate over each file
do
  taglist="$(tag --no-name --list "$file")" # get a comma-separated list (string) of tags
  IFS=',' read -ra tagarray <<< "$taglist" # convert that string to an array
  for tag in "${tagarray[@]}" # loop over that array of tags
  do
    exiftool -Keywords+="$tag" "$file" # add tag to file
  done
done

不要忘记通过执行以下操作使脚本可执行

chmod 755 /path/to/script/dir/photos.sh

https://github.com/jdberry/tag/安装代码,并从http://www.sno.phy.queensu.ca/~phil/exiftool/安装ExifTool。使用终端转到所选目录。此目录中只能包含“.jpg”文件,脚本不会递归迭代或更改其他文件类型。 成功的输出应该如下所示:

~ > cd /path/to/images/dir/
/path/to/images/dir/ > /path/to/script/dir/photos.sh
    1 image files updated
    1 image files updated

该脚本会将原始文件的副本保留为“img.jpg_original”。所有Apple标签都将从最终文件“img.jpg”中删除。在确定一切正常后(我使用Spotlight),请记得删除“原始”文件。