【问题标题】:Automator / AppleScript: Batch rotate and crop (and compress)Automator / AppleScript:批量旋转和裁剪(和压缩)
【发布时间】:2020-07-28 04:01:44
【问题描述】:

我正在尝试使用 Automator(或 AppleScript)创建一个工作流程,它一次对多个图像执行以下操作(您可能猜到我在 Mac 上)。

  1. 如果是纵向,则将图片旋转为横向
  2. 将图片裁剪为 2048x1536 像素(不添加黑色边框)
  3. (如果可能:压缩图像以减小文件大小)

有人知道如何做到这一点吗? 将所有这些都包含在一个 Automator 脚本/应用程序中会很棒,因为我有数千张需要旋转和裁剪的图像。

【问题讨论】:

  • 考虑将第 1 点和第 2 点发送至sips
  • 实际上,您可以通过 sips 完成所有这些操作。我很高兴为您编写一个脚本(尽管这不是一个脚本编写网站),但我需要一些细节。您希望裁剪居中,还是从某个角落裁剪?您是否希望首先在其中一个维度上缩放图像,因此裁剪仅从两侧起飞?如何处理小于这些尺寸的图像:为它们加边框、展开和裁剪,在不改变尺寸的情况下处理它们?
  • 感谢您的回答!作物应居中。不会有小于 2048x1536 像素的图像,但如果是这样,扩展它们是有意义的。所需的结果是正好为 2048x1536 像素且没有任何黑色边框的图像。
  • 如果图像是纵向的并且您将其旋转为横向,则有两种可能性,顺时针或逆时针。其中之一可能会使您的图片颠倒...
  • “压缩以减小文件大小” - 这取决于输入图像。它们是什么类型的文件 - TIFF、JPEG、PNG、PSD 文件?

标签: image macos image-processing applescript automator


【解决方案1】:

将此脚本归功于我被困在家里和无聊的事实。还要感谢 Apple,因为我使用了他们的一个模板文件的一部分(文件 → 从模板新建 → 液滴 → 递归图像文件处理模板)?

您可以通过两种方式运行以下脚本:

  • 直接从脚本编辑器:运行它,它会要求您选择要处理的文件夹
  • 作为 droplet:将脚本保存为应用程序(从“保存”屏幕的“格式”下拉菜单中选择“应用程序”),然后将文件和文件夹拖放到图标上

此脚本在文件夹层次结构中运行以查找图像文件,当它找到它们时,它会根据需要旋转、扩展和裁剪以使图像达到 2048x1536,它以原始文件名保存在同一文件夹中,并带有添加了标签“ - munged”。

在尝试此操作之前,请务必先备份您的图像文件夹。事情很少在第一次通过时就完美运行。

property type_list : {"JPEG", "TIFF", "PNGf", "8BPS", "BMPf", "GIFf", "PDF ", "PICT"}
property extension_list : {"jpg", "jpeg", "tif", "tiff", "png", "psd", "bmp", "gif", "jp2", "pdf", "pict", "pct", "sgi", "tga"}
property typeIDs_list : {"public.jpeg", "public.tiff", "public.png", "com.adobe.photoshop-image", "com.microsoft.bmp", "com.compuserve.gif", "public.jpeg-2000", "com.adobe.pdf", "com.apple.pict", "com.sgi.sgi-image", "com.truevision.tga-image"}

property target_height : 1536
property target_width : 2048
property marker_text : " - munged"

-- choose folders to process if the script is run from scriopt editor
on run
    set these_items to choose folder with prompt "Choose folders to process" with multiple selections allowed
    repeat with this_item in these_items
        process_item(POSIX path of this_item)
    end repeat
end run

-- dropped files and folders enter the stream here
on open these_items
    repeat with this_item in these_items
        process_item(POSIX path of this_item)
    end repeat
end open

-- this sub-routine parces folders to find image files
on process_item(this_item)
    tell application "System Events"
        set this_disk_item to disk item this_item
        -- skip invisible items and processed output
        if visible of this_disk_item is false or name of this_disk_item contains marker_text then return
        if class of this_disk_item is folder then
            set contents_list to disk items of this_disk_item
            repeat with an_item in contents_list
                my process_item(POSIX path of an_item)
            end repeat
        else
            try
                set this_extension to the name extension of this_disk_item
            on error
                set this_extension to ""
            end try
            try
                set this_filetype to the file type of this_disk_item
            on error
                set this_filetype to ""
            end try
            try
                set this_typeID to the type identifier of this_disk_item
            on error
                set this_typeID to ""
            end try
            if (class of this_disk_item is not alias) and ¬
                ((this_filetype is in the type_list) or ¬
                    (this_extension is in the extension_list) or ¬
                    (this_typeID is in typeIDs_list)) ¬
                    then
                my process_image(POSIX path of this_disk_item)
            end if
        end if
    end tell
end process_item

-- this sub-routine processes image files 
on process_image(this_image_path)
    tell application "Image Events"
        set revised_image to open this_image_path
        set {w, h} to dimensions of revised_image
        set desired_scale to target_height / target_width
        if w < h then
            -- portrait mode; rotate
            rotate revised_image to angle 270.0
            -- need to reverse dimensions; image events seems to be working off the saved file, not the rotated image
            set actual_scale to w / h
            if actual_scale > desired_scale then
                scale revised_image by factor target_width / h
            else
                scale revised_image by factor target_height / w
            end if
        else
            set actual_scale to h / w
            if actual_scale > desired_scale then
                scale revised_image by factor target_width / w
            else
                scale revised_image by factor target_height / h
            end if
        end if

        crop revised_image to dimensions {target_width, target_height}

        set new_file_name to my edited_file_name(name of revised_image)
        save revised_image as JPEG in ((POSIX path of (location of revised_image)) & "/" & new_file_name) with compression level medium with icon
        close revised_image
    end tell
end process_image

-- routine to add marker text to revised file names, so we don't overwrite the original data
on edited_file_name(the_file_name)
    set tid to my text item delimiters
    set my text item delimiters to "."
    set bits_list to text items of the_file_name
    if (count of bits_list) > 1 then
        set (item -2 of bits_list) to contents of (item -2 of bits_list) & marker_text
    else
        set (item 1 of bits_list) to contents of (item 1 of bits_list) & marker_text
    end if
    set revised_name to bits_list as text
    set my text item delimiters to tid
    return revised_name
end edited_file_name

【讨论】:

    猜你喜欢
    • 2020-02-01
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多