Running Scripts

class pgimp.GimpScriptRunner.GimpScriptRunner(environment: Dict[str, str] = None, working_directory='/home/docs/checkouts/readthedocs.org/user_builds/pgimp/checkouts/latest/doc/source')[source]

Bases: object

Executes python2 scripts within gimp’s python interpreter and is used to create higher-level functionality and abstractions that can be used in python3.

When the virtual framebuffer xvfb is installed, it will be automatically used and no other windowing system is required. This is important for batch jobs on machines that do not provide a graphical user interface.

Example:

>>> from pgimp.GimpScriptRunner import GimpScriptRunner
>>> GimpScriptRunner().execute('print("Hello from within gimp")')
'Hello from within gimp\n'
execute(string: str, parameters: Dict[str, Union[int, float, str, bytes, list, tuple, dict]] = None, timeout_in_seconds: float = None) → Optional[str][source]

Execute a given piece of code within gimp’s python interpreter.

Example:

>>> from pgimp.GimpScriptRunner import GimpScriptRunner
>>> GimpScriptRunner().execute('print("Hello from within gimp")')
'Hello from within gimp\n'
Parameters:
  • string – The code to be executed as string.
  • parameters – Parameter names and values. Supported types will be encoded as string, be passed to the script and be decoded there.
  • timeout_in_seconds – How long to wait for completion in seconds until a GimpScriptExecutionTimeoutException is thrown.
Returns:

The output produced by the script if no output stream is defined.

execute_and_parse_bool(string: str, parameters: dict = None, timeout_in_seconds: float = None) → bool[source]

Execute a given piece of code within gimp’s python interpreter and decode the result to bool.

Example:

>>> from pgimp.GimpScriptRunner import GimpScriptRunner
>>> GimpScriptRunner().execute_and_parse_bool(
...     'from pgimp.gimp.parameter import return_bool; return_bool("truthy")'
... )
True

See also execute().

execute_and_parse_json(string: str, parameters: dict = None, timeout_in_seconds: float = None) → Union[None, int, float, str, list, dict][source]

Execute a given piece of code within gimp’s python interpreter and decode the result to json.

Example:

>>> from pgimp.GimpScriptRunner import GimpScriptRunner
>>> GimpScriptRunner().execute_and_parse_json(
...     'from pgimp.gimp.parameter import return_json; return_json({"a": "b", "c": [1, 2]})'
... )['c']
[1, 2]

See also execute().

execute_binary(string: str, parameters: dict = None, timeout_in_seconds: float = None) → bytes[source]

Execute a given piece of code within gimp’s python interpreter and decode the result to bytes.

Example:

>>> import numpy as np
>>> from pgimp.GimpScriptRunner import GimpScriptRunner
>>> print(
...     np.frombuffer(
...         GimpScriptRunner().execute_binary(
...             "from pgimp.gimp.parameter import *; import sys; sys.stdout.write(get_bytes('arr'))",
...             parameters={"arr": np.array([i for i in range(0, 3)], dtype=np.uint8).tobytes()}),
...             dtype=np.uint8
...     )
... )
[0 1 2]

See also execute().

Returns:Raw bytes to be decoded to your target type.
execute_file(file: str, *, parameters: dict = None, timeout_in_seconds: float = None) → Optional[str][source]

Execute a script from a file within gimp’s python interpreter.

Example:

>>> from pgimp.GimpScriptRunner import GimpScriptRunner
>>> from pgimp.util.file import relative_to
>>> GimpScriptRunner().execute_file(relative_to(__file__, 'test-resources/hello.py'))
'Hello from within gimp\n'

See also execute().