def update_depth(self, image): if image is None: return ctx = Gdk.cairo_create(self.draw.get_window()) #ctx.set_source_rgb(0, 0, 0) #ctx.paint() view_width, view_height = self.view.get_size() sx = view_width / image.shape[1] sy = view_height / image.shape[0] ctx.scale(sx, sy) flat_image = image.flatten() #print(flat_image.min(), flat_image.max()) #flat_image -= flat_image.min() #try: # flat_image *= 255 / flat_image.max() #except RuntimeWarning: # return #flat_image = flat_image.astype(numpy.uint8) #print(flat_image.min(), flat_image.max()) flat_image = numpy.dstack((flat_image, flat_image, flat_image)) flat_image = numpy.insert(flat_image, 3, 255, 2) flat_image = flat_image.flatten() surface = cairo.ImageSurface.create_for_data(flat_image, cairo.FORMAT_ARGB32, image.shape[1], image.shape[0]) ctx.set_source_surface(surface) ctx.paint()
def set_image(self, image, i=0, dx=0, dy=0): ''' Add an image to the sprite. ''' while len(self.cached_surfaces) < i + 1: self.cached_surfaces.append(None) self._dx.append(0) self._dy.append(0) self._dx[i] = dx self._dy[i] = dy if isinstance(image, GdkPixbuf.Pixbuf) or \ isinstance(image, cairo.ImageSurface): w = image.get_width() h = image.get_height() else: w, h = image.get_size() if i == 0: # Always reset width and height when base image changes. self.rect.width = w + dx self.rect.height = h + dy else: if w + dx > self.rect.width: self.rect.width = w + dx if h + dy > self.rect.height: self.rect.height = h + dy if isinstance(image, cairo.ImageSurface): self.cached_surfaces[i] = image else: # Convert to Cairo surface surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self.rect.width, self.rect.height) context = cairo.Context(surface) context = Gdk.cairo_create(context) context.set_source_pixbuf(image, 0, 0) context.rectangle(0, 0, self.rect.width, self.rect.height) context.fill() self.cached_surfaces[i] = surface
def take_screenshot(): window = Gdk.get_default_root_window() surface = Gdk.cairo_create(window).get_target() with NamedTemporaryFile(prefix='autoaction_', suffix='.png') as f: surface.write_to_png(f.name) opencv_image = cv2.imread(f.name) return opencv_image
def onDrawEvent(self, *args): cr = Gdk.cairo_create(self.window.get_window()) cr.set_source_rgba(0, 0, 0, 0.7) cr.set_operator(cairo.OPERATOR_SOURCE) cr.paint() #cr.destroy() return False
def set_image(self, image, i=0, dx=0, dy=0): ''' Add an image to the sprite. ''' while len(self.cached_surfaces) < i + 1: self.cached_surfaces.append(None) self._dx.append(0) self._dy.append(0) self._dx[i] = dx self._dy[i] = dy if isinstance(image, GdkPixbuf.Pixbuf) or \ isinstance(image, cairo.ImageSurface): w = image.get_width() h = image.get_height() else: w, h = image.get_size() if i == 0: # Always reset width and height when base image changes. self.rect.width = w + dx self.rect.height = h + dy else: if w + dx > self.rect.width: self.rect.width = w + dx if h + dy > self.rect.height: self.rect.height = h + dy if isinstance(image, cairo.ImageSurface): self.cached_surfaces[i] = image else: # Convert to Cairo surface surface = cairo.ImageSurface( cairo.FORMAT_ARGB32, self.rect.width, self.rect.height) context = cairo.Context(surface) context = Gdk.cairo_create(context) context.set_source_pixbuf(image, 0, 0) context.rectangle(0, 0, self.rect.width, self.rect.height) context.fill() self.cached_surfaces[i] = surface
def expose (da, event): ctx = Gdk.cairo_create(da.window) ctx.set_source_rgb(0, 0, 0) ctx.set_line_width(SIZE / 4) ctx.set_tolerance(0.1) ctx.set_line_join(cairo.LINE_JOIN_ROUND) ctx.set_dash([SIZE/4.0, SIZE/4.0], 0) stroke_shapes(ctx, 0, 0) ctx.set_dash([], 0) stroke_shapes(ctx, 0, 3*SIZE) ctx.set_line_join(cairo.LINE_JOIN_BEVEL) stroke_shapes(ctx, 0, 6*SIZE) ctx.set_line_join(cairo.LINE_JOIN_MITER) stroke_shapes(ctx, 0, 9*SIZE) fill_shapes(ctx, 0, 12*SIZE) ctx.set_line_join(cairo.LINE_JOIN_BEVEL) fill_shapes(ctx, 0, 15*SIZE) ctx.set_source_rgb(1,0,0) stroke_shapes(ctx, 0, 15*SIZE)
def expose(da, event): ctx = Gdk.cairo_create(da.window) ctx.set_source_rgb(0, 0, 0) ctx.set_line_width(SIZE / 4) ctx.set_tolerance(0.1) ctx.set_line_join(cairo.LINE_JOIN_ROUND) ctx.set_dash([SIZE / 4.0, SIZE / 4.0], 0) stroke_shapes(ctx, 0, 0) ctx.set_dash([], 0) stroke_shapes(ctx, 0, 3 * SIZE) ctx.set_line_join(cairo.LINE_JOIN_BEVEL) stroke_shapes(ctx, 0, 6 * SIZE) ctx.set_line_join(cairo.LINE_JOIN_MITER) stroke_shapes(ctx, 0, 9 * SIZE) fill_shapes(ctx, 0, 12 * SIZE) ctx.set_line_join(cairo.LINE_JOIN_BEVEL) fill_shapes(ctx, 0, 15 * SIZE) ctx.set_source_rgb(1, 0, 0) stroke_shapes(ctx, 0, 15 * SIZE)
def clear_note(self): if self.last_shown_note: xpos = self.piano_xpos height = self.setting.noteysz ypos = self.note2ypos(self.last_shown_note) clip_extents = (xpos, ypos, self.width, ypos + height) self.draw_area(Gdk.cairo_create(self.get_window()), clip_extents) self.last_shown_note = None
def with_canvas(self, proc): hadj, vadj = self._gtk_adjustments() clip = rect_sized((hadj.value, vadj.value), self.size) # canvas = Canvas._from_gdk_drawable(self._gtk_inner_widget.bin_window) context = Gdk.cairo_create(self._gtk_inner_widget.get_bin_window()) self._gtk_prepare_cairo_context(context) canvas = Canvas._from_cairo_context(context) proc(canvas)
def get_preview(self): ''' Returns: str: with data ready to save with an image representing the state of the activity. Generally this is what the user is seeing in this moment. Activities can override this method, which should return a str with the binary content of a png image with a width of PREVIEW_SIZE pixels. The method does create a cairo surface similar to that of the canvas' window and draws on that. Then we create a cairo image surface with the desired preview size and scale the canvas surface on that. ''' if self.canvas is None or not hasattr(self.canvas, 'get_window'): return None window = self.canvas.get_window() alloc = self.canvas.get_allocation() dummy_cr = Gdk.cairo_create(window) target = dummy_cr.get_target() canvas_width, canvas_height = alloc.width, alloc.height screenshot_surface = target.create_similar(cairo.CONTENT_COLOR, canvas_width, canvas_height) del dummy_cr, target cr = cairo.Context(screenshot_surface) r, g, b, a_ = style.COLOR_PANEL_GREY.get_rgba() cr.set_source_rgb(r, g, b) cr.paint() self.canvas.draw(cr) del cr preview_width, preview_height = PREVIEW_SIZE preview_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, preview_width, preview_height) cr = cairo.Context(preview_surface) scale_w = preview_width * 1.0 / canvas_width scale_h = preview_height * 1.0 / canvas_height scale = min(scale_w, scale_h) translate_x = int((preview_width - (canvas_width * scale)) / 2) translate_y = int((preview_height - (canvas_height * scale)) / 2) cr.translate(translate_x, translate_y) cr.scale(scale, scale) cr.set_source_rgba(1, 1, 1, 0) cr.set_operator(cairo.OPERATOR_SOURCE) cr.paint() cr.set_source_surface(screenshot_surface) cr.paint() preview_str = StringIO.StringIO() preview_surface.write_to_png(preview_str) return preview_str.getvalue()
def capture_window(window): w, h = window.get_size() if h < MINIMUM_HEIGHT: window.set_default_size(w, DEFAULT_HEIGHT) view.reload() return src = Gdk.cairo_create(window.get_window()).get_target() src.write_to_png(filename) Gtk.main_quit()
def update_surface(self, last_tick, new_tick, surface): cr_ctx = Gdk.cairo_create(self.get_window()) ypos = self.height - surface.get_height() xpos = self.tick2xpos(last_tick) clip_extents = (xpos, ypos, xpos + surface.get_width(), ypos + surface.get_height()) self.draw_timeline(cr_ctx, clip_extents) xpos = self.tick2xpos(new_tick) cr_ctx.set_source_surface(surface, xpos, ypos) cr_ctx.paint()
def handle_key_press(key): tmp_dir = os.path.join(env.get_profile_path(), 'data') fd, file_path = tempfile.mkstemp(dir=tmp_dir) os.close(fd) window = Gdk.get_default_root_window() width, height = window.get_width(), window.get_height() window_cr = Gdk.cairo_create(window) window_surface = window_cr.get_target() screenshot_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) cr = cairo.Context(screenshot_surface) cr.set_source_surface(window_surface) cr.paint() screenshot_surface.write_to_png(file_path) client = GConf.Client.get_default() color = client.get_string('/desktop/sugar/user/color') content_title = None shell_model = shell.get_model() zoom_level = shell_model.zoom_level # TRANS: Nouns of what a screenshot contains if zoom_level == shell_model.ZOOM_MESH: content_title = _('Mesh') elif zoom_level == shell_model.ZOOM_GROUP: content_title = _('Group') elif zoom_level == shell_model.ZOOM_HOME: content_title = _('Home') elif zoom_level == shell_model.ZOOM_ACTIVITY: activity = shell_model.get_active_activity() if activity != None: content_title = activity.get_title() if content_title == None: content_title = _('Activity') if content_title is None: title = _('Screenshot') else: title = _('Screenshot of \"%s\"') % content_title jobject = datastore.create() try: jobject.metadata['title'] = title jobject.metadata['keep'] = '0' jobject.metadata['buddies'] = '' jobject.metadata['preview'] = _get_preview_data(screenshot_surface) jobject.metadata['icon-color'] = color jobject.metadata['mime_type'] = 'image/png' jobject.file_path = file_path datastore.write(jobject, transfer_ownership=True) finally: jobject.destroy() del jobject
def onDraw(self, widget, evt): """ Draw a gray background when pipeline is in NULL state. GStreamer takes care of this in the PAUSED and PLAYING states, otherwise, we simply draw a black rectangle to avoid garbage showing up. """ alloc = widget.get_allocation() cr = Gdk.cairo_create(widget.get_window()) cr.set_source_rgb (0.5, 0.5, 0.5); cr.rectangle (0, 0, alloc.width, alloc.height); cr.fill();
def _get_screenshot(self): """Copied from activity.get_preview() """ if self.canvas is None or not hasattr(self.canvas, 'get_window'): return None window = self.canvas.get_window() if window is None: return None alloc = self.canvas.get_allocation() dummy_cr = Gdk.cairo_create(window) target = dummy_cr.get_target() canvas_width, canvas_height = alloc.width, alloc.height screenshot_surface = target.create_similar(cairo.CONTENT_COLOR, canvas_width, canvas_height) del dummy_cr, target cr = cairo.Context(screenshot_surface) r, g, b, a_ = style.COLOR_PANEL_GREY.get_rgba() cr.set_source_rgb(r, g, b) cr.paint() self.canvas.draw(cr) del cr preview_width, preview_height = style.zoom(100), style.zoom(80) preview_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, preview_width, preview_height) cr = cairo.Context(preview_surface) scale_w = preview_width * 1.0 / canvas_width scale_h = preview_height * 1.0 / canvas_height scale = min(scale_w, scale_h) translate_x = int((preview_width - (canvas_width * scale)) / 2) translate_y = int((preview_height - (canvas_height * scale)) / 2) cr.translate(translate_x, translate_y) cr.scale(scale, scale) cr.set_source_rgba(1, 1, 1, 0) cr.set_operator(cairo.OPERATOR_SOURCE) cr.paint() cr.set_source_surface(screenshot_surface) cr.paint() preview_str = io.StringIO() preview_surface.write_to_png(preview_str) return preview_str.getvalue()
def expose_draw(self, wdiget, event, userdata=None): ''' 绘制透明窗口 ''' cr = Gdk.cairo_create(self.get_window()) if self.is_support_alpha: cr.set_source_rgba(1.0, 1.0, 1.0, 0.0) else: cr.set_source_rgba(1.0, 1.0, 1.0) cr.set_operator(cairo.OPERATOR_SOURCE) cr.paint() # 这里return False是使draw信号冒泡 return False
def expose_draw(self, widget, event, userdata=None): cr = Gdk.cairo_create(widget.get_window()) cr.scale(.2, .2) if self.supports_alpha: print("setting transparent window") cr.set_source_rgba(1.0, 1.0, 1.0, 0.0) else: print("setting opaque window") cr.set_source_rgb(1.0, 1.0, 1.0) cr.set_operator(cairo.OPERATOR_SOURCE) cr.paint() return False
def on_draw(self, widget, event, userdata=None): cr = Gdk.cairo_create(widget.get_window()) if self.supports_alpha: # print("setting transparent window") r = self.bgColor.red / 65536 g = self.bgColor.green / 65536 b = self.bgColor.blue / 65536 cr.set_source_rgba(r, g, b, self.opacity) else: # print("setting opaque window") cr.set_source_rgb(1.0, 1.0, 1.0) cr.set_operator(cairo.OPERATOR_SOURCE) cr.paint() return
def show_note(self, note): xpos = self.piano_xpos width = self.width - xpos height = self.setting.noteysz cr_ctx = Gdk.cairo_create(self.get_window()) if self.last_shown_note: ypos = self.note2ypos(self.last_shown_note) clip_extents = (xpos, ypos, self.width, ypos + height) self.draw_area(cr_ctx, clip_extents) ypos = self.note2ypos(note) cr_ctx.set_source_rgba(0.5, 0.5, 0.5, 0.5) cr_ctx.rectangle(xpos, ypos, width, height) cr_ctx.fill() self.last_shown_note = note
def update(self, image): if image is None: return ctx = Gdk.cairo_create(self.draw.get_window()) view_width, view_height = self.view.get_size() sx = view_width / image.shape[1] sy = view_height / image.shape[0] ctx.scale(sx, sy) # Need to insert an empty alpha channel value image_alpha = numpy.insert(image, 3, 255, 2) flat_image = image_alpha.flatten() surface = cairo.ImageSurface.create_for_data(flat_image, cairo.FORMAT_ARGB32, image.shape[1], image.shape[0]) ctx.set_source_surface(surface) ctx.paint()
def draw_image_head(self, widget, data): # Render the background we read from an image background_image = cairo.ImageSurface.create_from_png(self.IMAGE_HEAD_BACKGROUND) cairo_drawing_context = Gdk.cairo_create(widget.get_window()) cairo_drawing_context.set_source_surface(background_image, 0, 0) cairo_drawing_context.paint() # Text rendering with Pango is more involved but potentially # better for l10n and i18n purposes font_description = Pango.FontDescription() font_description.set_family(self.FONT) font_description.set_weight(Pango.Weight.NORMAL) font_description.set_absolute_size(12 * Pango.SCALE) pango_drawing_context = widget.get_pango_context() pango_text_layout = Pango.Layout(context=pango_drawing_context) pango_text_layout.set_font_description(font_description) pango_text_layout.set_text(self.TEXT, -1) # Color to render text cairo_drawing_context.set_source_rgb(0, 0, 0) # Position to render text cairo_drawing_context.move_to(75, 45) PangoCairo.show_layout(cairo_drawing_context, pango_text_layout)
def draw_image_head(self, widget, data): #Render the background we read from an image background_image = cairo.ImageSurface.create_from_png( self.IMAGE_HEAD_BACKGROUND) cairo_drawing_context = Gdk.cairo_create(widget.get_window()) cairo_drawing_context.set_source_surface(background_image,0,0) cairo_drawing_context.paint() #Text rendering with Pango is more involved but potentially #better for l10n and i18n purposes font_description = Pango.FontDescription() font_description.set_family(self.FONT) font_description.set_weight(Pango.Weight.NORMAL) font_description.set_absolute_size(12 * Pango.SCALE) pango_drawing_context = widget.get_pango_context() pango_text_layout = Pango.Layout(context=pango_drawing_context) pango_text_layout.set_font_description(font_description) pango_text_layout.set_text(self.TEXT, -1) #Color to render text cairo_drawing_context.set_source_rgb(0,0,0) #Position to render text cairo_drawing_context.move_to(75,45) PangoCairo.show_layout(cairo_drawing_context,pango_text_layout)
def draw(self, widget, event): ctx = Gdk.cairo_create(widget.get_window()) # print (widget) # #Gdk.Window.begin_draw_frame() # dctx = Gdk.Window.begin_draw_frame(widget.get_window()) # ctx = dctx.get_cairo_context() # Background // draw_background(ctx, bkg_color) ctx.set_source_rgb(*bkg_color) ctx.set_operator(cairo.OPERATOR_SOURCE) ctx.paint() # Bar # draw_bar(ctx, query_x, query_y, bar_w, bar_h, self.query, text_color, fontname, size=38) size = 38 ## query query_w = bar_w - 50 layout = PangoCairo.create_layout(ctx) font = Pango.FontDescription('%s %s' % (fontname, size)) layout.set_font_description(font) ctx.set_source_rgb(*text_color) layout.set_text(u'%s' % self.query, -1) PangoCairo.update_layout(ctx, layout) tw, th = layout.get_pixel_size() while tw > query_w: size = size - 1 font = Pango.FontDescription('%s %s' % (fontname, size)) layout.set_font_description(font) PangoCairo.update_layout(ctx, layout) tw, th = layout.get_pixel_size() ctx.move_to(query_x, query_y - 0.5 * th) PangoCairo.show_layout(ctx, layout) ## cursor cursor_x = query_x if self.query: cursor_x += calc_text_width(ctx, self.query[:self.cursor], size, fontname) ## FIX: slow ctx.set_source_rgb(*text_color) ctx.rectangle(cursor_x, 0.25*bar_h, 1.5, 0.50*bar_h) ctx.fill() ## mode if mode_labels.get(self.mode, ''): draw_text(ctx, 0.7*win_width, 15, 0.3*win_width-10, 0, mode_labels[self.mode], text_color, fontname, 10, justification='right') ## bar/menu separator draw_horizontal_separator(ctx, 0, bar_h-0.5, win_width, sep_color) # Menu items = self.items if not items: return ##self.draw_welcome_panel(ctx) else: n_items = len(items) max_items = min(5, n_items) item_selected_idx = self.item_selected left_w = win_width if not self.right_panel_visible else right_x first_item = 0 if (item_selected_idx < 5) else (item_selected_idx - 4) for pos in range(max_items): item = items[first_item+pos] is_selected = (first_item+pos == item_selected_idx) # pos -> (x, y) base_y = bar_h + pos * item_h self.draw_item(ctx, pos, base_y, item, is_selected, left_w) # if pos == 0: # draw_horizontal_separator(ctx, -5, base_y, left_w, sep_color) # if pos < 4 and not is_selected: # draw_horizontal_separator(ctx, 0, base_y+item_h, left_w, sep_color) ## Right panel if self.right_panel_visible: draw_rect(ctx, right_x, bar_h, right_w, menu_h, bkg_color) for pos, action in enumerate(self.right_items): base_y = bar_h + item_h * pos draw_horizontal_separator(ctx, right_x, base_y+81, right_w, sep_color) if self.right_item_selected == pos: draw_rect(ctx, right_x, base_y, right_w, 82, sel_color) draw_text(ctx, right_x+10, base_y, right_w, 82, action[0], seltext_color, fontname) else: draw_text(ctx, right_x+10, base_y, right_w, 82, action[0], text_color, fontname) draw_vertical_separator(ctx, right_x, bar_h, menu_h, sep_color) return False
def menu_action3(self, item, num): #print("menu action ", item, num) if num == 1: for aa in self.coll: print(aa.dump()) if num == 2: rstr = randstr(6) coord = Rectangle(self.mouse.x, self.mouse.y, 120, 120) self.add_rect(coord, rstr, randcolstr()) if num == 3: rstr = randstr(6) coord = Rectangle(self.mouse.x, self.mouse.y, 120, 120) self.add_romb(coord, rstr, randcolstr()) if num == 4: rstr = randstr(6) coord = Rectangle(self.mouse.x, self.mouse.y, 70, 70) self.add_circle(coord, rstr, randcolstr()) if num == 5: rstr = randstr(6) coord = Rectangle(self.mouse.x, self.mouse.y, 40, 40) self.add_text(coord, rstr, randcolstr()) if num == 6: rstr = randstr(6) coord = Rectangle(self.mouse.x, self.mouse.y, 40, 40) self.add_line(coord, rstr, randcolstr()) if num == 7: fff = "outline.pickle" #print("Saving to:", fff) sum = [] for aa in self.coll: sum.append(aa.dump()) ff = open(fff, "wb") pickle.dump(sum, ff) ff.close() if num == 8: fff = "outline.pickle" #print("Loading:", fff) self.readfile(fff) if num == 9: pass if num == 10: # Clear canvas self.coll = [] self.queue_draw() if num == 11: # crate PNG for aa in self.coll: aa.selected = False self.queue_draw() usleep(10) rect = self.get_allocation() #pixbuf = Gdk.pixbuf_get_from_window(self.get_window(), 0, 0, rect.width, rect.height) #self.surface = cairo.create_for_rectangle(0, 0, width, height) #self.surface = cairo.create_similar_image(cairo.Format.ARGB32, rect.width, rect.height) #cr = self.get_window().cairo_create() #cr = cairo.Context(self.surface) cr = Gdk.cairo_create(self.get_window()) self.draw_event(self, cr) pixbuf = Gdk.pixbuf_get_from_surface(cr.get_target(), 0, 0, rect.width, rect.height) pixbuf.savev("buff.png", "png", [None], []) if num == 12: print("Export") if num == 14: #print("Open") filter = Gtk.FileFilter.new() filter.add_pattern("*.ped") filter.set_name("PED files (*.ped)") filter2 = Gtk.FileFilter.new() filter2.add_pattern("*.*") filter2.set_name("ALL files (*.*)") filters = (filter2, filter) ofn = OpenFname(self.parewin.get_toplevel(), filters) fff = ofn.run() if not fff.fc_code: return print("Open filename", fff.fname) # Clear canvas self.coll = [] self.queue_draw() self.readfile(fff.fname) if num == 16: #print("Save") if self.fname == "untitled.ped": fff = self.file_dlg(Gtk.ResponseType.YES) else: self.writeout() if num == 17: #print("Save As") fff = self.file_dlg(Gtk.ResponseType.YES)
def draw_all(self): window = self.get_window() if window: self.do_draw(Gdk.cairo_create(window))