php 在MacOS上删除Alpha通道,合并斑点通道,并裁剪到边缘像素(没有ImageMagick)?

6kkfgxo0  于 2023-04-10  发布在  PHP
关注(0)|答案(1)|浏览(113)

我有一个用AppleScript创建的droplet应用程序(代码如下),它可以接受各种图像格式(无论是拖放到它上面还是通过对话框),并将PNG大小调整为用户定义的最大像素尺寸。与Photoshop批处理相比,它的速度快得像 lightning 。

寻找一些帮助(3)下面列出的限制.(我的首选是通过脚本/命令原生的MacOS;换句话说,无需安装ImageMagick。)
限制:
*Alpha通道和透明度,是否有办法丢弃所有Alpha通道?

  • 如果可能的话,如果有一个选项,以保持透明度,如果存在/希望。如果它需要被丢弃的工作,想要一个白色的背景,而不是黑色。
  • @MarkSetchell提供的PHP代码here可能是我解决方案的一部分,尽管我不够高级,不知道如何添加到下面的代码中。
    *专色通道。是否有方法将专色通道转换/合并为RGB?(专色通道是一种特殊颜色,例如Pantone 072 Blue,用于扩展图像的色域。出于这些目的,希望将其合并以获得RGB的一般表示。)
    *裁剪到边缘像素。如果需要,希望添加一个功能,可以将图像裁剪到图像边缘像素(与画布相反),最好在调整大小到用户提供的最大尺寸之前。
    当前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

任何对上述(3)限制的帮助都将是惊人的!

基本上我正在寻找的是图像的预览,就好像它在Photoshop,Illustrator,InDesign等中打开一样(MacOS Finder预览也不能正确预览)。
下面的链接带您到参考文件...原始PSD与Alpha通道,斑点通道和透明度,沿着3个PNG示例,这将是所需的输出...一个是相同的大小,另一个修剪到最外面的像素,另一个在图像边缘周围填充10 px。
要在Photoshop中做到这一点,过程是转换为RGB(特别是如果在CMYK更大的色域),合并斑点通道,放弃Alpha通道,修剪所有侧面(可选),保存为PNG。这种“快速”合并斑点的方法不保持透明度。上面的脚本负责其余部分,除了填充回边框,如果需要的话。

  • Original_Spot_Alpha.tif
  • Out_flat_trim_pad.png
  • Out_flat_trim.png
  • Out_flat.png

Click here to view/download reference files

zxlwwiss

zxlwwiss1#

下面是脚本的更新版本,它应该解决第一个(背景将是白色的)和第三个限制:

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
    
    -- Remove alpha channel (transparency) and set the background color to white
    set the_command to "sips --flatten --setProperty backgroundColor '255 255 255 1' " & quoted form of newPNG_name & " --out " & quoted form of newPNG_name
    log "the_command: " & the_command
    do shell script the_command
    
    -- Crop image to edge pixels
    tell application "Image Events"
        set the_image to open newPNG_name
        crop the_image to (crop box the_image)
        save the_image in newPNG_name as "PNG"
        close the_image
    end tell
    
on error error_message
    display dialog error_message
end try
end process_item

要处理spot通道,您需要安装Ghostscript,因为AppleScript不支持处理spot通道。

相关问题