I'm trying to use functions of gimp 2.8.22 to convert a pdf to jpeg and i want to do that with a python script using the gimpfu library from my windows cmd (i have installed python 3.6.1).
Right now, i't trying to do that with an example script:
#!/usr/bin/env python
# Hello World in GIMP Python
from gimpfu import *
def hello_world(initstr, font, size, color) :
# First do a quick sanity check on the font
if font == 'Comic Sans MS' :
initstr = "Comic Sans? Are you sure?"
# Make a new image. Size 10x10 for now -- we'll resize later.
img = gimp.Image(1, 1, RGB)
# Save the current foreground color:
pdb.gimp_context_push()
# Set the text color
gimp.set_foreground(color)
# Create a new text layer (-1 for the layer means create a new layer)
layer = pdb.gimp_text_fontname(img, None, 0, 0, initstr, 10,
True, size, PIXELS, font)
# Resize the image to the size of the layer
img.resize(layer.width, layer.height, 0, 0)
# Background layer.
# Can't add this first because we don't know the size of the text layer.
background = gimp.Layer(img, "Background", layer.width, layer.height,
RGB_IMAGE, 100, NORMAL_MODE)
background.fill(BACKGROUND_FILL)
img.add_layer(background, 1)
# Create a new image window
gimp.Display(img)
# Show the new image window
gimp.displays_flush()
# Restore the old foreground color:
pdb.gimp_context_pop()
register(
"python_fu_hello_world",
"Hello world image",
"Create a new image with your text string",
"Akkana Peck",
"Akkana Peck",
"2010",
"Hello world (Py)...",
"", # Create a new image, don't work on an existing one
[
(PF_STRING, "string", "Text string", 'Hello, world!'),
(PF_FONT, "font", "Font face", "Sans"),
(PF_SPINNER, "size", "Font size", 50, (1, 3000, 1)),
(PF_COLOR, "color", "Text color", (1.0, 0.0, 0.0))
],
[],
hello_world, menu="<Image>/File/Create")
main()
I try to run the script from my cmd like this:
gimp-2.8 --no-interface --batch '(python_fu_hello_world RUN-NONINTERACTIVE "Hello" Arial 50 red)' -b '(gimp-quit 1)'
However, no matter what i do, i always geht the same error message:
(gimp-2.8:1020): LibGimpBase-WARNING **: gimp-2.8: gimp_wire_read(): error
edit: Ok, thanks. I left out the interface statement and I also tried out the most simple example to figure out whats wrong:
#!/usr/bin/env python
# Hello World in GIMP Python
from gimpfu import *
def hello_world():
gimp.message("Hello, GIMP world!\n")
register(
"hello_world",
'A simple Python-Fu "Hello, World" plug-in',
'When run this plug-in prints "Hello, GIMP world!" in a dialog box.',
"Tony Podlaski",
"Tony Podlaski 2017. MIT License",
"2017",
"Hello World",
"",
[],
[],
hello_world,
menu="<Image>/Filters/HelloWorld",
)
main()
The script actually works when i run it from Gimp itself, but when i try to run it from my cmd, Gimp opens with another cmd saying: Error: ( : 1) eval: unbound variable: hello_world
Does someone know what i'm missing here?
Because the script you are using creates the image and then shows it in a window.... But you are calling Gimp with the
--no-interface
flag so the window is not shown.IMHO to convert from PDF to JPEG, ImageMagick's
convert
command would be a lot simpler.Also, in Windows Gimp comes with its own built-in Python 2.7 interpreter, so you have to write your Python code for that version, and there is no need to install another one.
To run a python script you don't need to have it register as a plugin. IMHO you should even avoid this, this unnecessarily pollutes Gimp's menus and procedure name space. Here is an example:
The batch script (saved as
batch.py
):To call it:
The parameters in slow-motion:
-idf
: work without user interface, and load neither data nor fonts (you may perhaps need to keep the fonts to load pdfs)--batch-interpreter python-fu-eval
: whatever follows-b
is Python, not script-fu"import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')"
: the code that we ask Gimp to execute, namely:import sys;sys.path=['.']+sys.path;
: extend the import path to include the current directoryimport batch;
: import the file with our script, which is now in a directory which is part of the path.batch.run('./images')
: call therun()
function of thebatch
module we imported, giving it the name of a directory containing the images to process.-b "pdb.gimp_quit(1)"
: another piece of python: exit when done.Note how the command line cleverly uses double and single quotes to pass all parameters to Gimp and then to Python(*). And yes, you can use forward slashes as file separators in Windows.
To debug, matters are bit complicated in Windows because there is no always a stdout stream. Things that can help:
-i
parameter temporarily so that you get the UI and perhaps a chance to see messages.--verbose
which makes Gimp start a secondary console window.(*) In a Linux/OSX shell one would do the opposite: single quotes for the shell, double quotes for Python.