我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用gi.repository.GdkPixbuf()。
def get_icon(image, size): """ Generate a GdkPixbuf image :param image: icon name or image path :return: GdkPixbux Image """ directory = path.join(env.get("DATA_DIR"), "applications", "images") + "/" theme = Gtk.IconTheme.get_default() if theme.has_icon(path.splitext(image)[0]): icon = theme.load_icon(path.splitext(image)[0], size, 0) elif path.exists(directory + image): icon = GdkPixbuf.Pixbuf.new_from_file(directory + image) elif path.exists(image): icon = GdkPixbuf.Pixbuf.new_from_file(image) else: icon = theme.load_icon("image-missing", size, 0) if icon.get_width() != size or icon.get_height() != size: icon = icon.scale_simple(size, size, GdkPixbuf.InterpType.BILINEAR) return icon
def get_backlight(self): subprocess.call(shlex.split(COMMAND)) pixbuf = GdkPixbuf.Pixbuf.new_from_file('/tmp/fswebcam.jpg') image = Image.frombytes('RGB', (pixbuf.get_width(), pixbuf.get_height()), pixbuf.get_pixels()) pixbuf.savev('/home/lorenzo/Escritorio/test.png', "png", [], []) return calculation.calculate_brightness_for_image(image)
def get_icon(icon_path): """ Generate a GdkPixbuf image :param image: icon name or image path :return: GdkPixbux Image """ try: if "symbolic" in icon_path: icon = (None, icon_path) else: icon = GdkPixbuf.Pixbuf.new_from_file(icon_path) if icon.get_width() != 48 or icon.get_height() != 48: icon = icon.scale_simple(48, 48, GdkPixbuf.InterpType.BILINEAR) return icon except GLib.Error: return None
def drawn(self, widget, cr): if self._draw_image is None: return with self._size_lock: img = self._draw_image if self._show_orientation: img = img.copy() self._orient_scaled.composite( img, 0, 0, img.props.width, img.props.height, 0, 0, 1, 1, GdkPixbuf.InterpType.NEAREST, 215) img = img.rotate_simple(self._rotation) rect = self.get_allocation() Gdk.cairo_set_source_pixbuf(cr, img, (rect.width - img.props.width) // 2, (rect.height - img.props.height) // 2) cr.paint() self._draw_pending.clear()
def get_about_dialog(self): """Create and populate the about dialog.""" about_dialog = Gtk.AboutDialog() about_dialog.set_name(comun.APPNAME) about_dialog.set_version(comun.VERSION) about_dialog.set_copyright( 'Copyrignt (c) 2014-2016\nLorenzo Carbonell Cerezo') about_dialog.set_comments(_('An indicator for Pomodoro Technique')) about_dialog.set_license(''' This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. ''') about_dialog.set_website('http://www.atareao.es') about_dialog.set_website_label('http://www.atareao.es') about_dialog.set_authors([ 'Lorenzo Carbonell <https://launchpad.net/~lorenzo-carbonell>']) about_dialog.set_documenters([ 'Lorenzo Carbonell <https://launchpad.net/~lorenzo-carbonell>']) about_dialog.set_translator_credits(''' Lorenzo Carbonell <https://launchpad.net/~lorenzo-carbonell>\n ''') about_dialog.set_icon(GdkPixbuf.Pixbuf.new_from_file(comun.ICON)) about_dialog.set_logo(GdkPixbuf.Pixbuf.new_from_file(comun.ICON)) about_dialog.set_program_name(comun.APPNAME) return about_dialog # ##################### callbacks for the menu #######################
def get_about_dialog(self): """Create and populate the about dialog.""" about_dialog = Gtk.AboutDialog() about_dialog.set_name(comun.APPNAME) about_dialog.set_version(comun.VERSION) about_dialog.set_copyright( 'Copyrignt (c) 2016\nLorenzo Carbonell Cerezo') about_dialog.set_comments(_('An indicator to set backlight')) about_dialog.set_license(''' This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. ''') about_dialog.set_website('http://www.atareao.es') about_dialog.set_website_label('http://www.atareao.es') about_dialog.set_authors([ 'Lorenzo Carbonell <https://launchpad.net/~lorenzo-carbonell>']) about_dialog.set_documenters([ 'Lorenzo Carbonell <https://launchpad.net/~lorenzo-carbonell>']) about_dialog.set_translator_credits(''' Lorenzo Carbonell <https://launchpad.net/~lorenzo-carbonell>\n ''') about_dialog.set_icon(GdkPixbuf.Pixbuf.new_from_file(comun.ICON)) about_dialog.set_logo(GdkPixbuf.Pixbuf.new_from_file(comun.ICON)) about_dialog.set_program_name(comun.APPNAME) return about_dialog # ##################### callbacks for the menu #######################
def load_image(filename, format='png'): loader = GdkPixbuf.PixbufLoader.new_with_type(format) loader.write(pkg_resources.resource_string(__name__, filename)) loader.close() return loader.get_pixbuf()
def resized(self, widget, rect): with self._size_lock: if self._rotation in (0, 180): ratio = min( rect.width / self._board_full.props.width, rect.height / self._board_full.props.height) else: ratio = min( rect.width / self._board_full.props.height, rect.height / self._board_full.props.width) ratio = min(ratio, 1.0) # never resize larger than native if ratio != self._ratio: # Only resize if necessary (plenty of resizes wind up with the # same ratio) self._board_scaled = self._board_full.scale_simple( self._board_full.props.width * ratio, self._board_full.props.height * ratio, GdkPixbuf.InterpType.BILINEAR) self._grid_scaled = self._grid_full.scale_simple( self._grid_full.props.width * ratio, self._grid_full.props.height * ratio, GdkPixbuf.InterpType.BILINEAR) self._orient_scaled = self._orient_full.scale_simple( self._orient_full.props.width * ratio, self._orient_full.props.height * ratio, GdkPixbuf.InterpType.BILINEAR) self._ratio = ratio
def _update_run(self): # This method runs in the background _update_thread while True: # Only update the screen if do_draw's finished the last update; # this effectively serves to "drop frames" if the system's too # busy if self._draw_pending.wait(self.screen_update_delay): # The wait period above enforces the maximum update rate; if # a draw is still pending, wait on the stop event instead if self._stop.wait(self.screen_update_delay): break else: # Only update if the screen's modification timestamp indicates # that the data has changed since last time ts = self._screen_client.timestamp if ts > self._draw_timestamp: with self._size_lock: img = self._board_scaled.copy() pixels = GdkPixbuf.Pixbuf.new_from_bytes( GLib.Bytes.new(self._screen_client.rgb_array.tostring()), colorspace=GdkPixbuf.Colorspace.RGB, has_alpha=False, bits_per_sample=8, width=8, height=8, rowstride=8 * 3) pixel_rect = Gdk.Rectangle() pixel_rect.x = int(126 * self._ratio) pixel_rect.y = int(155 * self._ratio) pixel_rect.width = int(512 * self._ratio) pixel_rect.height = pixel_rect.width pixels.composite( img, pixel_rect.x, pixel_rect.y, pixel_rect.width, pixel_rect.height, pixel_rect.x, pixel_rect.y, # Why 8.1? With 8.0 (which is what it should be), # registration errors crop up at the far right (try # it and see); no idea why 8.1 is required to # correct them, but I'm too knackered to argue with # Gdk any more... pixel_rect.width / 8.1, pixel_rect.height / 8.1, GdkPixbuf.InterpType.NEAREST, 255) self._grid_scaled.composite( img, pixel_rect.x, pixel_rect.y, pixel_rect.width, pixel_rect.height, pixel_rect.x, pixel_rect.y, 1, 1, GdkPixbuf.InterpType.NEAREST, 255) self._draw_image = img self._draw_timestamp = ts self._draw_pending.set() # Schedule a redraw when the app is next idle; like Gtk # methods, Gdk methods must only be called from the main # thread (otherwise the app locks up) try: GLib.idle_add(self.props.window.invalidate_rect, None, False) except AttributeError: # Our Gdk window has been destroyed; don't whinge, just # exit the thread as we're obviously shutting down break