向图像添加剪切路径信息

时间:2019-06-06 11:48:33

标签: java php python go command-line-interface

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

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

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

我尝试过使用ImagickDraw,pathStart ... pathFinish,但它在图像上绘制的内容不是我在GIMP中可以看到的路径,例如

enter image description here

编辑:感谢使用其他语言的解决方案。

3 个答案:

答案 0 :(得分:2)

发布之前的基于Java的答案后,我想知道是否有可能以某种方式编写gimp脚本以完成我们想要的事情。事实证明,这是可能的,而且非常容易!

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

#!/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()

之后,您可以在没有用户界面的情况下以批处理模式启动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中读取批处理命令。这样,它就保持打开状态,您只需写入stdin就可以向它发送新的“ add-path”命令。

gimp -i -b -

答案 1 :(得分:1)

这个答案是关于如何用Java完成的。不幸的是,还没有100%就绪的解决方案,您需要自己实施一些事情。

TIFF的剪切路径扩展是Adobe的专有扩展。基本上,他们使用自定义TIFF标签(34377/Photoshop)沿原始图像数据存储Photoshop路径元素。

TwelveMonkeys图像库具有用于直接从tiff Images读取photoshop路径格式的实现。

不幸的是,它不支持编写photoshop路径数据。这是您必须自己实现的部分。这是reading the path data的相关代码。格式也很好documented here。根据您的用例,您可能不需要实现完整的路径编写支持,而仅需要您需要的部分即可。

Here is a good related post关于编写多层tiff图像,其中包含有关如何用Java构造和编写最终图像的信息。

答案 2 :(得分:0)

TwelveMonkeys图像库3.5版包含用于读取和写入 Adob​​e Photoshop剪切路径(支持PSD,JPEG和TIFF)的功能。使用类Paths作为起点。

将此依赖项添加到您的Java项目中:

<dependency>
  <groupId>com.twelvemonkeys.imageio</groupId>
  <artifactId>imageio-clippath</artifactId>
  <version>${twelvemonkeys.version}</version> <!-- 3.5 for now -->
</dependency>
相关问题