def badge_png(ds): """ make a badge image based on real data >>> badge_png(test_dataset())[1:4] 'PNG' """ # This is for badge_png preview image: try: import poppler except: err_missing('Python-poppler library not installed; can\'t generate preview.') try: from gtk.gdk import Pixbuf,COLORSPACE_RGB except: err_missing( 'Library not installed: gtk.gdk; can\'t generate preview.') ###### # get pdf pdf = mkpdf(ds,sample=True) # render pdf onto pixbuf pixbuf = Pixbuf(COLORSPACE_RGB,False,8,286,225) doc = poppler.document_new_from_data(pdf,len(pdf),password='') page0=doc.get_page(0) page0.render_to_pixbuf(0,0,8,11,1,0,pixbuf) # save pixbuf as png # There has to be a better way to get the image? lst=[] pixbuf.save_to_callback(lambda b,l: l.append(b), 'png', user_data=lst) png=''.join(lst) return png
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 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 motion_notify_event(self, win, event): """Mouse motion_notify_event handler""" pixbuf = Pixbuf(COLORSPACE_RGB, False, 8, 1, 1) root = get_default_root_window() xcoord, ycoord = event.get_root_coords() from_draw = pixbuf.get_from_drawable(root, root.get_colormap(), int(xcoord), int(ycoord), 0, 0, 1, 1) pixel = from_draw.get_pixels_array()[0][0] self.rgb = (pixel[0], pixel[1], pixel[2]) self.draw_color() self.label.set_label(rgb_to_string(self.rgb).upper())
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 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 create_blank_pixbuf(size=16): pix = Pixbuf(COLORSPACE_RGB, True, 8, size, size) pix.fill(0x0) return pix