编写 GIMP Python 脚本

问题描述 投票:0回答:1

我想从 Python 脚本打开 GIMP(可能带有

subprocess.Popen
),然后 GIMP 应该启动一个 Python 脚本来打开图像并添加图层。我怎样才能做到这一点?我这样做了:

subprocess.Popen(["gimp", "--batch-interpreter" , "python-fu-eval" , "-b" ,"\'import sys; sys.path.append(\"/home/antoni4040\"); import gimpp; from gimpfu import *; gimpp.main()\'"])

但是即使控制台说:

批量命令执行成功

什么也没发生。

from gimpfu import *

def gimpp():
    g = gimp.pdb
    images = gimp.image_list()
    my_image = images[0]
    layers = my_image.layers
    new_image = g.gimp_file_load_layer("/home/antoni4040/Έγγραφα/Layout.png")
    my_image.add_layer(new_image)
    new_layer = g.gimp_layers_new(my_image,  1024, 1024, RGBA_IMAGE, "PaintHere", 0, NORMAL_MODE)
    my_image.add_layer(new_layer)

register('GimpSync', "Sync Gimp with Blender", "", "", "", "", "<Image>/SyncWithBlender", '*', [], [], gimpp)
main()
python gimp gimpfu python-fu
1个回答
13
投票

好吧,我终于成功了。我使用 GIMP Python 脚本 创建了一个 gimp 插件,它可以做很多事情,包括你提到的层。然后,您可以从命令行运行 gimp,将参数传递给 python gimp 脚本。文章“在 Gimp 批处理模式中使用 Python-Fu”是学习如何从命令行调用 gimp 插件的绝佳资源。下面的例子将加载指定的图像到gimp中,水平翻转,保存并退出gimp。 flip.py 是 gimp 插件,应该放在你的插件目录中,在我的例子中是 ~/.gimp-2.6/plug-ins/flip.py。

翻转.py

from gimpfu import pdb, main, register, PF_STRING from gimpenums import ORIENTATION_HORIZONTAL def flip(file): image = pdb.gimp_file_load(file, file) drawable = pdb.gimp_image_get_active_layer(image) pdb.gimp_image_flip(image, ORIENTATION_HORIZONTAL) pdb.gimp_file_save(image, drawable, file, file) pdb.gimp_image_delete(image) args = [(PF_STRING, 'file', 'GlobPattern', '*.*')] register('python-flip', '', '', '', '', '', '', '', args, [], flip) main()

从终端可以运行这个:

gimp -i -b '(python-flip RUN-NONINTERACTIVE "/tmp/test.jpg")' -b '(gimp-quit 0)'

或从 Windows cmd:

gimp-console.exe -i -b "(python-flip RUN-NONINTERACTIVE """<test.jpg>""")" -b "(gimp-quit 0)"

或者您可以使用以下命令从 python 脚本运行相同的内容:

from subprocess import check_output cmd = '(python-flip RUN-NONINTERACTIVE "/tmp/test.jpg")' output = check_output(['/usr/bin/gimp', '-i', '-b', cmd, '-b', '(gimp-quit 0)']) print output

我测试了两者以确保它们有效。每个脚本运行后,您应该会看到图像被翻转。

© www.soinside.com 2019 - 2024. All rights reserved.