File: xml-graph.py

package info (click to toggle)
gegl 1%3A0.4.66-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,032 kB
  • sloc: ansic: 241,723; cpp: 4,529; lisp: 3,614; xml: 2,724; python: 1,208; ruby: 1,205; sh: 182; makefile: 35; javascript: 6
file content (88 lines) | stat: -rwxr-xr-x 2,394 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
from __future__ import print_function

from gi.repository import Gegl
import sys

def check_operations(required_ops):
  known_ops = Gegl.list_operations()

  missing_ops = []

  for op in required_ops:
    if not op in known_ops:
      print("Could not find required operation:", op)
      missing_ops.append(op)

  return not missing_ops

out_file_name = "xml-graph-output.png"

text_xml = """
<?xml version='1.0' encoding='UTF-8'?>
<gegl>
  <node operation='gegl:text'>
    <params>
      <param name='string'>Hello World</param>
      <param name='color'>rgb(1.0, 1.0, 1.0)</param>
      <param name='size'>24</param>
    </params>
  </node>
</gegl>
"""

background_xml = """
<?xml version='1.0' encoding='UTF-8'?>
<gegl>
  <node operation='gegl:plasma'>
    <params>
      <param name='width'>256</param>
      <param name='height'>128</param>
    </params>
  </node>
</gegl>
"""

if __name__ == '__main__':
  Gegl.init(sys.argv)

  if not check_operations(["gegl:png-save", "gegl:text", "gegl:plasma", "gegl:translate"]):
    sys.exit(1)

  graph = Gegl.Node()

  # Load the graphs from xml, if they were in external files
  # we would use Gegl.Node.new_from_file() instead.
  lower_graph = Gegl.Node.new_from_xml(background_xml, "/")
  upper_graph = Gegl.Node.new_from_xml(text_xml, "/")

  # Add a reference from our main node to the xml graphs so they
  # can't go out of scope as long as "graph" is alive.
  graph.add_child(lower_graph)
  graph.add_child(upper_graph)

  # Center the upper graph on the lower graph
  text_bbox = upper_graph.get_bounding_box()
  background_bbox = lower_graph.get_bounding_box()

  x_offset = max((background_bbox.width - text_bbox.width) / 2, 0)
  y_offset = max((background_bbox.height - text_bbox.height) / 2, 0)

  translate = graph.create_child("gegl:translate")
  translate.set_property("x", x_offset)
  translate.set_property("y", y_offset)

  upper_graph.connect_to("output", translate, "input")

  # Use the "gegl:over" to combine the two graphs
  over = graph.create_child("gegl:over")
  lower_graph.connect_to("output", over, "input")
  translate.connect_to("output", over, "aux")

  # Save the result to a png file
  save_node = graph.create_child ("gegl:png-save")
  save_node.set_property("path", out_file_name)
  over.connect_to("output", save_node, "input")
  save_node.process()

  print("Save to", out_file_name)