MacOS原生脚本实现移除Alpha通道、合并专色通道及裁剪边缘像素
AppleScript图片处理Droplet功能优化求助
我用AppleScript编写了一个Droplet应用(代码附后),支持拖放或通过对话框选择多种格式图片,输出按用户定义最大像素尺寸调整后的PNG文件,处理速度远快于Photoshop批处理。现寻求帮助解决以下3项功能局限,优先采用macOS原生脚本/命令实现,无需安装ImageMagick:
- Alpha通道与透明度:能否移除所有Alpha通道?若可提供保留透明度的选项更佳;若必须移除Alpha通道,需将背景设为白色而非黑色。我参考过Stack Overflow上@MarkSetchell提供的PHP代码,但不知如何整合到现有脚本中。
- 专色通道(Spot Channel):能否将专色通道转换/合并至RGB通道?(专色通道是用于拓展色域的特殊颜色通道,如潘通072蓝,需合并后得到RGB下的通用呈现效果)
- 裁剪边缘像素:添加按需裁剪至图像边缘像素(而非画布尺寸)的功能,最好在调整至用户设定的最大像素尺寸前完成。
本质上希望图片呈现效果与在Photoshop、Illustrator、InDesign等软件中打开的一致(macOS Finder预览无法正确显示)。参考文件包含带Alpha通道、专色通道及透明度的原文件,以及3种期望输出的PNG示例。
当前AppleScript代码:
property extension_list : {"tif", "tiff", "png", "jpeg", "jpg", "pict", "psb", "pdf", "bmp", "eps", "psd", "heic"} global current_folder global pixel_size --___________________________________________________________________________ drag and drop on open these_items log "open" log these_items try tell application "Finder" set maxpixel_size to display dialog "Enter desired maximum pixel dimension below." default answer "600" buttons {"Cancel", "Continue"} default button "Continue" with icon note set pixel_size to (text returned of the result) end tell process(these_items, pixel_size) delay 2 display notification with title "Image Conversions Complete" on error error_message tell me to "exit" end try end open --___________________________________________________________________________ files on process(these_items, pixel_size) initialise() log "process " & these_items repeat with i from 1 to the count of these_items set this_item to (item i of these_items) set the item_info to info for this_item if (alias of the item_info is false) and ¬ (the name extension of the item_info is in the extension_list) then process_item(this_item, pixel_size) end if end repeat finalise() end process --___________________________________________________________________________ folder on process_folder(this_folder, pixel_size) initialise() set current_folder to this_folder log "process_folder " & this_folder set these_items to list folder this_folder without invisibles repeat with i from 1 to the count of these_items set this_item to alias ((this_folder as text) & (item i of these_items)) set the item_info to info for this_item if (alias of the item_info is false) and ¬ (the name extension of the item_info is in the extension_list) then process_item(this_item, pixel_size) end if end repeat finalise() end process_folder on initialise() log "initialise" end initialise on finalise() log "finalise" end finalise --___________________________________________________________________________ sips process on process_item(item_alias, pixel_size) log "process_item: " & item_alias set this_path to (item_alias) as string set the newPNG_name to my add_extension(item_alias, "png") try set the_command to "sips --setProperty format png --resampleHeightWidthMax " & pixel_size & " " & quoted form of POSIX path of item_alias & " --out " & quoted form of newPNG_name log "the_command: " & the_command do shell script the_command on error error_message display dialog error_message end try end process_item --____________________________________________________________________________ default behavior try tell application "Finder" set the source_folder to choose folder with prompt ¬ "Pick a folder to process:" set maxpixel_size to display dialog "What is the maximum pixel size?" default answer "600" buttons {"Continue"} default button "Continue" with icon note set pixel_size to (text returned of the result) end tell process_folder(source_folder, pixel_size) delay 2 display notification with title "Image Conversions Complete" on error error_message tell me to "exit" end try --________________________________________________________________________________ file ext on add_extension(item_alias, new_extension) log "add_extension" log item_alias set this_info to the info for item_alias --set this_name to the name of this_info --set this_name to (item_alias) as string set this_name to POSIX path of item_alias set this_extension to the name extension of this_info if this_extension is missing value then set the default_name to this_name else set the default_name to text 1 thru -((length of this_extension) + 2) of this_name end if return (the default_name & "." & the new_extension) end add_extension
内容的提问来源于stack exchange,提问作者Chris
相关产品推荐
相关产品推荐

