-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_print.py
44 lines (36 loc) · 1.29 KB
/
test_print.py
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
#!/usr/bin/python
from gi.repository import Gtk
import Image, StringIO, cairo
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World Printing")
self.button = Gtk.Button(label="Print A Rectangle")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
def on_button_clicked(self, widget):
pd = Gtk.PrintOperation()
pd.set_n_pages(1)
pd.connect("draw_page", self.draw_page)
result = pd.run(
Gtk.PrintOperationAction.PRINT_DIALOG, None)
print result # handle errors etc.
def draw_page(self, operation=None, context=None, page_nr=None):
ctx = context.get_cairo_context()
w = context.get_width()
h = context.get_height()
# ctx.set_source_rgb(0.5, 0.5, 1)
#ctx.rectangle(w*0.1, h*0.1, w*0.8, h*0.8)
#ctx.stroke()
# Getting JPG image
im = Image.open('output/carnets/18084850.jpg')
buf = StringIO.StringIO()
im.save(buf, format="PNG")
buf.seek(0)
imgsf = cairo.ImageSurface.create_from_png(buf)
ctx.set_source_surface(imgsf, 0.5, 0.5)
ctx.paint()
return
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()