How do I output info to the console in a Gimp python script?

孤街醉人 提交于 2019-12-21 09:31:45

问题


I've just started learning about Gimp scripting using Python, and was wondering, how do I output text to the console? I am using version 2.7.5 on Windows 7.

I tried the print function, but it does not write anything to the python-fu console or the dev console that launches with Gimp. Is there a function I should use to do this? or is this a problem with the 2.7.5 release? I found a few mentions of "gimp-message" but that seems to be a function used with Scheme (Script-fu)

Thanks!

(also posted as a thread here)


回答1:


Use:

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

However... this shows in the Error console if the console window is up, else in a message dialog with an OK button, waiting for user acknowledgment. So you can really use it only once or twice in the script...

There is also

pdb.gimp_progress_set_text('This goes to the status bar')

That goes to the status bar (IIRC) and to the plugin progress dialog, if any, but is rather temporary.

You can also use plain print statements for debug purposes. On Linux their output shows up in the terminal from which you started Gimp, and on Windows they may appear in gimp-console if you started Gimp that way (so the general user won't see anything unless you really tell them where to look).




回答2:


We can redirect stdout and stderr.

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



回答3:


Printing froma python script will just print to GIMP`s stdout channel - it is possible that in Windows you have to start GIMP itself from the command line, instead of starting it from the menu.



来源:https://stackoverflow.com/questions/9955834/how-do-i-output-info-to-the-console-in-a-gimp-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!