13

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)

morten.c
  • 3,414
  • 5
  • 40
  • 45
Aralox
  • 1,441
  • 1
  • 24
  • 44

3 Answers3

10

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()
Massimo Fuccillo
  • 337
  • 7
  • 10
8

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

xioned
  • 96
  • 1
2

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.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • (at least on linux:) python print goes to stdout (at least, to sys.stdout, which is no longer fd 1); and in the gui python console, that goes via pipe to the window; in a batch run it goes to gimp's stdout. Gimp runs python as a separate process (not embedded); try this: gimp -i --batch-interpreter=python-fu-eval -b 'import os; os.system("ps %s" % os.getpid()); pdb.gimp_quit(1)' – greggo Jan 18 '13 at 18:24