【问题标题】:Combine images with ImageMagick using Applescript in Automator在 Automator 中使用 Applescript 将图像与 ImageMagick 合并
【发布时间】:2016-03-06 10:12:47
【问题描述】:

所以我有一个 PDF 文件,我正在使用 Automator 从每张幻灯片生成 JPG 文件。然后我想遍历这些图像中的每一个并从中创建并排的照片。所以第 1 页和第 2 页将创建一个图像,然后是第 3 页和第 4 页,依此类推...到目前为止,我有一个 Applescript 操作,其中输入是所有图像文件:

on run {input, parameters}

    set selectedFiles to {}

    repeat with i in input
        copy (POSIX path of i) to end of selectedFiles
    end repeat

    return selectedFiles
end run

我想要做的是这个函数,这是一个 ImageMagick 函数,两个合并到彼此相邻的图像:

do shell script "convert " +append 01.jpg 02.jpg 01&2.jpg

如何在上面的 Applescript 中添加此功能?

或者也许使用 Shell 脚本会更容易?

【问题讨论】:

    标签: shell imagemagick applescript automator


    【解决方案1】:

    我有点着急,所以不是我最好的代码,但你可以在bash 中这样做:

    #!/bin/bash
    # pdfsheets
    #
    # Passs the name of a PDF as parameter and get it as a bunch of double-page spreads called "sheet-0.jpg" ... "sheet-n.jpg"
    #
    # Pick up parameter
    pdf=$1
    echo "Processing document: $pdf"
    
    # Split document into individual pages each as JPEG
    convert "$pdf" page-$$-%03d.jpg
    
    # Get names of pages into array and see how many we got
    pages=( $(ls page-$$-*.jpg) )
    npages=${#pages[@]}
    echo DEBUG:npages:$npages
    
    # Check if odd number of pages - synthesize empty white one at end if odd
    if [ $((npages%2)) -ne 0 ]; then
       lastpage=${pages[@]:(-1)}
       echo DEBUG:lastpage:$lastpage
       newlast=$(printf "page-$$-%03d.jpg" $npages)
       convert "$lastpage" -threshold -1 $newlast
       pages=( $(ls page-$$-*.jpg) )
       npages=${#pages[@]}
    fi
    s=0
    for ((i=0;i<npages;i+=2)) do
       a=${pages[i]}
       b=${pages[((i+1))]}
       out=$(printf "sheet-%d.jpg" $s)
       echo Converting $a and $b to $out
       convert $a $b +append $out
       ((s++))
    done
    

    这样运行,得到如下输出:

    ./pdfsheets document.pdf
    Processing document: document.pdf
    DEBUG:npages:5
    DEBUG:lastpage:page-49169-004.jpg
    Converting page-49169-000.jpg and page-49169-001.jpg to sheet-0.jpg
    Converting page-49169-002.jpg and page-49169-003.jpg to sheet-1.jpg
    Converting page-49169-004.jpg and page-49169-005.jpg to sheet-2.jpg
    

    【讨论】:

    • 谢谢!我尝试了你的建议。我猜在第 6 行 $ 是 #,所以我改变了它。但仍然给我一个错误:pastebin.com/tJb33jNp
    • 嗯……奇怪! $ 应该是 #,是的。它在我的机器上运行良好。你能运行identify /Users/visztpeter/Downloads/document.pdf 看看是否能在你的PDF 中找到一些页面吗?
    • 如果做不到这一点,你可以把第一行改成#!/bin/bash -xv再运行一次,你就会得到各种调试。
    猜你喜欢
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    相关资源
    最近更新 更多