dounao4179 2019-06-06 11:48
浏览 108
已采纳

向图像添加剪切路径信息

I'm trying to add a clipping path to a TIFF image. I made one TIFF file with GIMP that contains a clipping path and I can clip my image using it by

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

But I would like to append clipping path info as coordinates like GIMP does into the image file itself so later another process can read it...

I've tried with ImagickDraw, pathStart... pathFinish but it draws something on image not as a path that I can see in GIMP like

enter image description here

Edit: Solutions in other languages are appreciated.

  • 写回答

2条回答 默认 最新

  • drsl90685154 2019-06-15 23:24
    关注

    After posting the previous java based answer, I was wondering if it would be possible to script gimp in a way to do what we want. Turns out this is possible and quite easy!

    First install the following gimp plugin wich loads the image, draws the path and then saves the image as tif. Copy it to your gimp plugins folder. On Mac this is ~/Library/Application Support/GIMP/2.10/plug-ins/addpath.py. Create the plug-ins folder if it doesn't exist yet. Also make sure the python file is executable by the user who runs gimp (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()
    

    After that, you can start gimp without user interface in batch mode, executing the plugin.

    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)'

    Without the second -b '(gimp-quit 0)' gimp keeps running. You can also ask gimp to read the batch commands from stdin. That way it stays open and you can send new "add-path" commands to it simply by writing to stdin.

    gimp -i -b -

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看