def transform_pixbuf(p, dest_size, scale): dest = Pixbuf(p.props.colorspace, p.props.has_alpha, p.props.bits_per_sample, dest_size, dest_size) dest.fill(0) s = dest_size * scale / max(p.get_width(), p.get_height()) offset_x = int(round((dest_size - p.get_width() * s) / 2)) offset_y = int(round((dest_size - p.get_height() * s) / 2)) dest_width = int(round(min(dest_size - offset_x, s * p.get_width()))) dest_height = int(round(min(dest_size - offset_y, s * p.get_height()))) p.scale(dest, offset_x, offset_y, dest_width, dest_height, offset_x, offset_y, s, s, INTERP_HYPER) return dest
def setup_box(name, r = 0.0, g = 0.0, b = 1.0): """Does all the heavy lifting of setting up a box in a pix buffer.""" color_depth = 8 icon_width = 128 icon_height = 64 pixbuf = Pixbuf(gtk.gdk.COLORSPACE_RGB, # color space True, # has alpha color_depth, # color depth icon_width, # width icon_height) # height # pixbuf = gtk.gdk.pixbuf_new_from_file_at_size("./images/slave_icon.png", 64, 128) pix_data = pixbuf.get_pixels_array() surface = cairo.ImageSurface.create_for_data(pix_data, \ cairo.FORMAT_RGB24, \ pixbuf.get_width(), \ pixbuf.get_height(), \ pixbuf.get_rowstride()) color = gtk.gdk.Color(0x0000, 0x0000, 0xFFFF) # Blue cr = cairo.Context(surface) cr.set_operator(cairo.OPERATOR_OVER) cr.set_source_rgba(color16_to_cairo(b * 0xFFFF), \ color16_to_cairo(g * 0xFFFF), \ color16_to_cairo(r * 0xFFFF), \ 1.0) # alpha = 1.0 cr.rectangle(0, 0, icon_width, icon_height) cr.fill() cr.set_source_rgb(0.0, 0.0, 0.0) cr.rectangle(0, 0, icon_width, icon_height) cr.set_line_width(5.0) cr.stroke() cr.set_source_rgba(0.0, 0.0, 0.0, 1.0) cr.set_font_size(10) x_bearing, y_bearing, twidth, theight = cr.text_extents(name)[:4] text_x = 0.5 - twidth / 2 - x_bearing text_y = 0.5 - theight / 2 - y_bearing pos_x = 0 + (icon_width / 2.0) + text_x pos_y = 0 + (icon_height / 2.0) + text_y cr.move_to(pos_x, pos_y) cr.show_text(name) surface.flush() surface.finish() return pixbuf
def grab_to_file(self, filename): ''' based on: http://stackoverflow.com/questions/69645/take-a-screenshot-via-a-python-script-linux http://www.pygtk.org/docs/pygtk/class-gdkpixbuf.html only "jpeg" or "png" ''' w = get_default_root_window() sz = w.get_size() #print "The size of the window is %d x %d" % sz pb = Pixbuf(COLORSPACE_RGB, False, 8, sz[0], sz[1]) # 24bit RGB pb = pb.get_from_drawable(w, w.get_colormap(), 0, 0, 0, 0, sz[0], sz[1]) assert pb type = "png" if filename.endswith('.jpeg'): type = "jpeg" pb.save(filename, type)
def create_blank_pixbuf(size=16): pix = Pixbuf(COLORSPACE_RGB, True, 8, size, size) pix.fill(0x0) return pix