1 | import gi |
---|
2 | gi.require_version('Gtk', '3.0') |
---|
3 | gi.require_version('PangoCairo', '1.0') |
---|
4 | |
---|
5 | import cairo |
---|
6 | |
---|
7 | from gi.repository import Gtk,GdkPixbuf,GLib,Gdk, PangoCairo, Pango |
---|
8 | |
---|
9 | |
---|
10 | def scale_image(image_file,x,y,aspect_ratio=True): |
---|
11 | |
---|
12 | image=Gtk.Image.new_from_file(image_file) |
---|
13 | pixbuf=image.get_pixbuf() |
---|
14 | if not pixbuf: |
---|
15 | image=Gtk.Image.new_from_file("/usr/share/icons/Vibrancy-Colors/status/96/image-missing.png") |
---|
16 | pixbuf=image.get_pixbuf() |
---|
17 | img_x=pixbuf.get_width() |
---|
18 | img_y=pixbuf.get_height() |
---|
19 | if aspect_ratio: |
---|
20 | |
---|
21 | ratio=min(x*1.0/img_x,y*1.0/img_y) |
---|
22 | else: |
---|
23 | ratio=1 |
---|
24 | img_x=x |
---|
25 | img_y=y |
---|
26 | |
---|
27 | pixbuf=pixbuf.scale_simple(img_x*ratio,img_y*ratio,GdkPixbuf.InterpType.HYPER) |
---|
28 | img=Gtk.Image.new_from_pixbuf(pixbuf) |
---|
29 | |
---|
30 | return img |
---|
31 | |
---|
32 | #def scale_image |
---|
33 | |
---|
34 | |
---|
35 | def scale_pixbuf(pixbuf,x,y,aspect_ratio=True): |
---|
36 | |
---|
37 | img_x=pixbuf.get_width() |
---|
38 | img_y=pixbuf.get_height() |
---|
39 | if aspect_ratio: |
---|
40 | |
---|
41 | ratio=min(x*1.0/img_x,y*1.0/img_y) |
---|
42 | else: |
---|
43 | ratio=1 |
---|
44 | img_x=x |
---|
45 | img_y=y |
---|
46 | |
---|
47 | pixbuf=pixbuf.scale_simple(img_x*ratio,img_y*ratio,GdkPixbuf.InterpType.BILINEAR) |
---|
48 | return pixbuf |
---|
49 | |
---|
50 | #def scale_pixbuf |
---|
51 | |
---|
52 | |
---|
53 | def create_banner(icon_file,size_x,size_y,txt,frame=True,output_file=None): |
---|
54 | |
---|
55 | reduction_percentage=0.65 |
---|
56 | if not frame: |
---|
57 | reduction_percentage=0.9 |
---|
58 | |
---|
59 | offset_x= (size_x - (size_x*reduction_percentage)) /2 |
---|
60 | offset_y= (size_y - (size_y*reduction_percentage)) /2 |
---|
61 | |
---|
62 | image=scale_image(icon_file,size_x*reduction_percentage,size_y*reduction_percentage,frame) |
---|
63 | pixbuf=image.get_pixbuf() |
---|
64 | |
---|
65 | surface=cairo.ImageSurface(cairo.FORMAT_ARGB32,size_x,size_y) |
---|
66 | ctx=cairo.Context(surface) |
---|
67 | |
---|
68 | lg1 = cairo.LinearGradient(0.0,0.0, 0.0, size_y) |
---|
69 | lg1.add_color_stop_rgba(0, 0/255.0, 95/255.0, 219/255.0, 1) |
---|
70 | lg1.add_color_stop_rgba(1, 0/255.0, 56/255.0, 134/255.0, 1) |
---|
71 | ctx.rectangle(0, 0, size_x, size_y) |
---|
72 | ctx.set_source(lg1) |
---|
73 | ctx.fill() |
---|
74 | |
---|
75 | img_offset_x=offset_x |
---|
76 | img_offset_y=offset_y |
---|
77 | |
---|
78 | label_space_percentage=1.5 |
---|
79 | if txt==None: |
---|
80 | label_space_percentage=1 |
---|
81 | |
---|
82 | Gdk.cairo_set_source_pixbuf(ctx,image.get_pixbuf(),img_offset_x,img_offset_y/label_space_percentage) |
---|
83 | ctx.paint() |
---|
84 | |
---|
85 | if txt!=None and len(txt) >1: |
---|
86 | |
---|
87 | ctx.set_source_rgba( 0,0,0,0.7 ) |
---|
88 | ctx.rectangle(0, size_y-offset_y , size_x, offset_y) |
---|
89 | ctx.fill() |
---|
90 | |
---|
91 | pctx = PangoCairo.create_layout(ctx) |
---|
92 | desc = Pango.font_description_from_string ("Roboto %s"%int(offset_y*0.5)) |
---|
93 | pctx.set_font_description(desc) |
---|
94 | ctx.set_source_rgba(0.9,0.9,0.9,1) |
---|
95 | |
---|
96 | if len(txt)> 18: |
---|
97 | txt=txt[0:15]+"..." |
---|
98 | |
---|
99 | pctx.set_markup("%s"%txt) |
---|
100 | text_x,text_y=pctx.get_pixel_size() |
---|
101 | ctx.move_to(size_x/2 - text_x/2, size_y - offset_y ) |
---|
102 | PangoCairo.show_layout(ctx, pctx) |
---|
103 | |
---|
104 | px=Gdk.pixbuf_get_from_surface(surface,0,0,size_x,size_y) |
---|
105 | img=Gtk.Image.new_from_pixbuf(px) |
---|
106 | |
---|
107 | |
---|
108 | if output_file == None: |
---|
109 | return [True,img] |
---|
110 | else: |
---|
111 | surface.write_to_png(output_file) |
---|
112 | return [True,output_file] |
---|
113 | |
---|
114 | |
---|
115 | #def create_banner |
---|
116 | |
---|
117 | if __name__=="__main__": |
---|
118 | |
---|
119 | create_banner("/usr/share/icons/Antu/apps/48/mozilla-firefox.svg",200,200,"Firefox") |
---|