如何在Gimp Python脚本中将信息输出到控制台?

13
我刚开始学习使用Python编写Gimp脚本,想知道如何将文本输出到控制台? 我正在Windows 7上使用2.7.5版本。 我尝试了print函数,但它没有将任何内容写入到python-fu控制台或启动Gimp的dev控制台中。 我应该使用哪个函数才能实现此功能?还是这是2.7.5版本的问题? 我发现有几个提到“gimp-message”的地方,但那似乎是用于Scheme(Script-fu)的函数。 谢谢! (也作为一个主题发布here
3个回答

10

我们可以重定向标准输出和标准错误。

#!/usr/bin/env python
# coding: iso-8859-1

from gimpfu import *
import sys
sys.stderr = open( 'c:\\temp\\gimpstderr.txt', 'w')
sys.stdout = open( 'c:\\temp\\gimpstdout.txt', 'w')

def MyUsefulFilter(img, drw):

    # these print redirected to gimpstdout.txt
    print 'hello world'
    print img
    print drw

    # this error redirected to gimpstderr.txt
    x = 0
    y = 1/x


    pdb.gimp_image_undo_group_start(img)
    # useful code here
    pdb.gimp_image_undo_group_end(img)


register(
    "useful_filter",
    "very useful indeed",
    "",
    "MF",
    "GPL",
    "2013",
    "<Image>/Filters/Photo/Useful Filter",
    "RGB*",
    [],
    [],
    MyUsefulFilter)

main()

8

使用:

pdb.gimp_message('This is displayed as a message')

然而...... 如果控制台窗口打开,这会在错误控制台中显示,否则会在带有“确定”按钮的消息对话框中等待用户确认。因此,在脚本中只能使用一两次......
还有另外一个方法:
pdb.gimp_progress_set_text('This goes to the status bar')

这个输出会显示在状态栏(如果我没记错的话)和插件进度对话框中,但是它是相当临时的。

你也可以使用纯文本打印语句进行调试。在Linux上,它们的输出会显示在启动Gimp的终端中,在Windows上,如果您以这种方式启动了Gimp,则可能会出现在gimp-console中(因此一般用户不会看到任何东西,除非您真正告诉他们去哪里查找)。


2
从Python脚本中打印只会打印到GIMP的标准输出通道 - 在Windows中,您可能需要从命令行启动GIMP本身,而不是从菜单启动它。

(至少在Linux中)Python的打印输出到标准输出(stdout)(至少是sys.stdout,不再是fd 1);在GUI Python控制台中,通过管道传输到窗口;在批处理运行中,它会进入GIMP的stdout。GIMP作为一个独立进程运行Python(而不是嵌入式);尝试这个命令:gimp - i --batch-interpreter = python-fu-eval -b 'import os; os.system("ps %s" % os.getpid()); pdb.gimp_quit(1)' - greggo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接