本文介绍了编写 GIMP python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是从 python 程序(可能使用 subprocess.Popen)打开 gimp,同时,gimp 将从一个 python 脚本开始,该脚本将打开一个图像并添加一个层......好吧,我怎样才能做到这一点(我希望 GIMP 有更好的文档...)?

What I want to do is to open gimp from a python program (with subprocess.Popen, perhaps), and in the same time, gimp will start with a python script that will open an image and add a layer... Well, how can I achieve that(I wish GIMP had better documentation...)?

更新:

我这样做了: subprocess.Popen(["gimp", "--batch-interpreter" , "python-fu-eval" , "-b" ,"\'import sys; sys.path.append(\"/home/antoni4040\"); import gimpp; from gimpfu import *; gimpp.main()\'"]) ,但是,即使控制台说批处理命令执行成功",也没有发生...

I did this: subprocess.Popen(["gimp", "--batch-interpreter" , "python-fu-eval" , "-b" ,"\'import sys; sys.path.append(\"/home/antoni4040\"); import gimpp; from gimpfu import *; gimpp.main()\'"]) ,but, even if the console says "batch command executed successfully", nothing happens...

更新 2:

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

推荐答案

好的,我终于搞定了.我使用 GIMP Python 脚本 来创建一个 gimp 插件,它可以做很多包括你提到的层的东西.然后,您可以从命令行运行 gimp,将参数传递给 python gimp 脚本.文章 在 Gimp Batch 中使用 Python-FuMode 是学习如何从命令行调用 gimp 插件的极好资源.下面的示例将指定图像加载到 gimp,水平翻转,保存并退出 gimp.

Ok I finally got it working. I used the GIMP Python scripting to create a gimp plugin which can do a tonne of stuff including the layers you mentioned. Then you can just run gimp from the command line passing arguments to the python gimp script. The article Using Python-Fu in Gimp Batch Mode was an excellent resource for learning how to call gimp plugins from the command line. The example below will load the specified image into gimp, flip it horizontally, save and exit gimp.

flip.py 是 gimp 插件,应该放在你的插件目录中,在我的例子中是 ~/.gimp-2.6/plug-ins/flip.py.

flip.py is the gimp plug-in and should be placed in your plug-ins directory which was ~/.gimp-2.6/plug-ins/flip.py in my case.

flip.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:

or from Windows cmd:

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

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

or you can run the same from a python script using:

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

我对两者都进行了测试以确保它们能正常工作.每次运行脚本后,您应该会看到图像被翻转.

I tested both to make sure they work. You should see the image get flipped after each script run.

这篇关于编写 GIMP python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 19:51