Update Portuguese translation
[gegl.git] / examples / xml-graph.py
bloba2e97a37243c97bfb429781eaa8efce6dde57acd
1 #!/usr/bin/env python
2 from __future__ import print_function
4 from gi.repository import Gegl
5 import sys
7 def check_operations(required_ops):
8 known_ops = Gegl.list_operations()
10 missing_ops = []
12 for op in required_ops:
13 if not op in known_ops:
14 print("Could not find required operation:", op)
15 missing_ops.append(op)
17 return not missing_ops
19 out_file_name = "xml-graph-output.png"
21 text_xml = """
22 <?xml version='1.0' encoding='UTF-8'?>
23 <gegl>
24 <node operation='gegl:text'>
25 <params>
26 <param name='string'>Hello World</param>
27 <param name='color'>rgb(1.0, 1.0, 1.0)</param>
28 <param name='size'>24</param>
29 </params>
30 </node>
31 </gegl>
32 """
34 background_xml = """
35 <?xml version='1.0' encoding='UTF-8'?>
36 <gegl>
37 <node operation='gegl:plasma'>
38 <params>
39 <param name='width'>256</param>
40 <param name='height'>128</param>
41 </params>
42 </node>
43 </gegl>
44 """
46 if __name__ == '__main__':
47 Gegl.init(sys.argv)
49 if not check_operations(["gegl:png-save", "gegl:text", "gegl:plasma", "gegl:translate"]):
50 sys.exit(1)
52 graph = Gegl.Node()
54 # Load the graphs from xml, if they were in external files
55 # we would use Gegl.Node.new_from_file() instead.
56 lower_graph = Gegl.Node.new_from_xml(background_xml, "/")
57 upper_graph = Gegl.Node.new_from_xml(text_xml, "/")
59 # Add a reference from our main node to the xml graphs so they
60 # can't go out of scope as long as "graph" is alive.
61 graph.add_child(lower_graph)
62 graph.add_child(upper_graph)
64 # Center the upper graph on the lower graph
65 text_bbox = upper_graph.get_bounding_box()
66 background_bbox = lower_graph.get_bounding_box()
68 x_offset = max((background_bbox.width - text_bbox.width) / 2, 0)
69 y_offset = max((background_bbox.height - text_bbox.height) / 2, 0)
71 translate = graph.create_child("gegl:translate")
72 translate.set_property("x", x_offset)
73 translate.set_property("y", y_offset)
75 upper_graph.connect_to("output", translate, "input")
77 # Use the "gegl:over" to combine the two graphs
78 over = graph.create_child("gegl:over")
79 lower_graph.connect_to("output", over, "input")
80 translate.connect_to("output", over, "aux")
82 # Save the result to a png file
83 save_node = graph.create_child ("gegl:png-save")
84 save_node.set_property("path", out_file_name)
85 over.connect_to("output", save_node, "input")
86 save_node.process()
88 print("Save to", out_file_name)