向图像添加剪切路径信息

tan*_*din 9 php python java command-line-interface go

我正在尝试为TIFF图像添加剪切路径。我制作了一个包含剪切路径的TIFF文件,GIMP我可以使用它来剪切图像

$img = new Imagick("./test.tiff");
$img->clipPathImage("#1", false);
Run Code Online (Sandbox Code Playgroud)

但我想像GIMP一样将剪切路径信息作为坐标附加到图像文件本身中,以便以后其他进程可以读取它...

我已经尝试过使用ImagickDraw,pathStart ... pathFinish,但是它在图像上绘制了一些内容,而不是像我在GIMP中可以看到的那样

在此处输入图片说明

编辑:其他语言的解决方案表示赞赏。

Flo*_*ann 2

在发布了之前基于 java 的答案之后,我想知道是否可以以某种方式编写 gimp 脚本来执行我们想要的操作。事实证明这是可能的,而且非常简单!

首先安装以下 gimp 插件,该插件加载图像,绘制路径,然后将图像保存为 tif。将其复制到您的 gimp 插件文件夹中。在 Mac 上,这是~/Library/Application Support/GIMP/2.10/plug-ins/addpath.py. plug-ins如果该文件夹尚不存在,则创建该文件夹。还要确保运行 gimp ( ) 的用户可以执行 python 文件chmod u+x addpath.py

#!/usr/bin/env python

from gimpfu import pdb, main, register, PF_STRING

def add_path(infile, outfile):
    image = pdb.gimp_file_load(infile, 'image')
    vectors = pdb.gimp_vectors_new(image, 'clippath')
    w = image.width
    h = image.height
    path = [
        # The array of bezier points for the path.
        # You can modify this for your use-case.
        # This one draws a rectangle 10px from each side.
        # Format: control1-x, control1-y, center-x, center-y, control2-x, control2-y
        10, 10, 10, 10, 10, 10,
        w - 10, 10, w - 10, 10, w - 10, 10,
        w - 10, h - 10, w - 10, h - 10, w - 10, h - 10,
        10, h - 10, 10, h - 10, 10, h - 10
    ]
    pdb.gimp_vectors_stroke_new_from_points(vectors, 0, len(path), path, True)
    pdb.gimp_image_add_vectors(image, vectors, 0)
    drawable = pdb.gimp_image_get_active_layer(image)
    pdb.file_tiff_save(image, drawable, outfile, 'image.tif', 0)

args = [(PF_STRING, 'infile', 'GlobPattern', '*.*'), (PF_STRING, 'outfile', 'GlobPattern', '*.*')]
register('python-add-path', '', '', '', '', '', '', '', args, [], add_path)

main()
Run Code Online (Sandbox Code Playgroud)

之后,您可以在批处理模式下启动没有用户界面的gimp,执行插件。

gimp -i -b '(python-add-path RUN-NONINTERACTIVE "/absolute/path/to/your/input/file.png" "/absolute/path/to/the/tif/file.tif")' -b '(gimp-quit 0)'

没有第二个-b '(gimp-quit 0)'gimp就继续跑。您还可以要求 gimp 从 stdin 读取批处理命令。这样它就保持打开状态,您只需写入标准输入即可向其发送新的“添加路径”命令。

gimp -i -b -