def get_widget_screenshot(widget):
window = widget.get_window()
if not window: return None
allocation = widget.get_allocation()
pixbuf = Gdk.pixbuf_get_from_window(
window,
allocation.x,
allocation.y,
allocation.width,
allocation.height
)
if not pixbuf:
return None
else:
return pixbuf
python类pixbuf_get_from_window()的实例源码
def __pixel_at(x, y):
"""Returns (r, g, b) color code for a pixel with given coordinates (each value is in 0..256 limits)"""
root_window = gdk.get_default_root_window()
buf = gdk.pixbuf_get_from_window(root_window, x, y, 1, 1)
pixels = buf.get_pixels()
if type(pixels) == type(""):
rgb = tuple([int(byte.encode('hex'), 16) for byte in pixels])
else:
rgb = tuple(pixels)
return rgb
def take_screenshot(widget):
w = Gdk.get_default_root_window()
left, top = widget.get_position()
width, height = widget.get_size()
pixbuf = Gdk.pixbuf_get_from_window(w, left, top, width, height)
return pixbuf
def __pixel_at(x, y):
"""Returns (r, g, b) color code for a pixel with given coordinates (each value is in 0..256 limits)"""
root_window = Gdk.get_default_root_window()
if not root_window:
return tuple([0, 0, 0])
buf = Gdk.pixbuf_get_from_window(root_window, x, y, 1, 1)
pixels = buf.get_pixels()
if isinstance(pixels, str):
rgb = tuple([int(byte.encode('hex'), 16) for byte in pixels])
else:
rgb = tuple(pixels)
return rgb
def __pixel_at(x, y):
#Returns (r, g, b) color code for a pixel with given coordinates (each value is in 0..256 limits)
root_window = gdk.get_default_root_window()
buf = gdk.pixbuf_get_from_window(root_window, x, y, 1, 1)
pixels = buf.get_pixels()
if type(pixels) == type(""):
rgb = tuple([int(byte.encode('hex'), 16) for byte in pixels])[0:3]
else:
rgb = tuple(pixels)[0:3]
return rgb
def __pixel_at(x, y):
"""Returns (r, g, b) color code for a pixel with given
coordinates (each value is in 0..256 limits)"""
root_window = gdk.get_default_root_window()
buf = gdk.pixbuf_get_from_window(root_window, x, y, 1, 1)
pixels = buf.get_pixels()
if type(pixels) == type(""):
rgb = tuple([int(byte.encode('hex'), 16) for byte in pixels])
else:
rgb = tuple(pixels)
return rgb
def screenshot(gdk_window):
wait()
time.sleep(0.5)
pb = Gdk.pixbuf_get_from_window(
gdk_window, 0, 0, gdk_window.get_width(), gdk_window.get_height()
)
return pixbuf2image(pb)
def screenshot(file='screenshot.png', timeStamp=True):
"""
This function wraps the ImageMagick import command to take a screenshot.
The file argument may be specified as 'foo', 'foo.png', or using any other
extension that ImageMagick supports. PNG is the default.
By default, screenshot filenames are in the format of foo_YYYYMMDD-hhmmss.png .
The timeStamp argument may be set to False to name the file foo.png.
"""
if not isinstance(timeStamp, bool):
raise TypeError("timeStampt must be True or False")
# config is supposed to create this for us. If it's not there, bail.
assert os.path.isdir(config.scratchDir)
baseName = ''.join(file.split('.')[0:-1])
fileExt = file.split('.')[-1].lower()
if not baseName:
baseName = file
fileExt = 'png'
if timeStamp:
ts = TimeStamp()
newFile = ts.fileStamp(baseName) + '.' + fileExt
path = config.scratchDir + newFile
else:
newFile = baseName + '.' + fileExt
path = config.scratchDir + newFile
from gi.repository import Gdk
from gi.repository import GdkPixbuf
rootWindow = Gdk.get_default_root_window()
geometry = rootWindow.get_geometry()
pixbuf = GdkPixbuf.Pixbuf(colorspace=GdkPixbuf.Colorspace.RGB,
has_alpha=False,
bits_per_sample=8,
width=geometry[2],
height=geometry[3])
pixbuf = Gdk.pixbuf_get_from_window(rootWindow, 0, 0, geometry[2], geometry[3])
# GdkPixbuf.Pixbuf.save() needs 'jpeg' and not 'jpg'
if fileExt == 'jpg':
fileExt = 'jpeg'
try:
pixbuf.savev(path, fileExt, [], [])
except GLib.GError:
raise ValueError("Failed to save screenshot in %s format" % fileExt)
assert os.path.exists(path)
logger.log("Screenshot taken: " + path)
return path