/usr/lib/python3/dist-packages/autopilot/display/_screenshot.py is in python3-autopilot 1.5.1+16.04.20160412-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Autopilot Functional Test Tool
# Copyright (C) 2014 Canonical
#
# 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/>.
#
"""This module contains support for capturing screenshots."""
import logging
import os
import subprocess
import time
import tempfile
from io import BytesIO
from PIL import Image
import autopilot._glib
logger = logging.getLogger(__name__)
def get_screenshot_data(display_type):
"""Return a BytesIO object of the png data for the screenshot image.
*display_type* is the display server type. supported values are:
- "X11"
- "MIR"
:raises RuntimeError: If attempting to capture an image on an unsupported
display server.
:raises RuntimeError: If saving image data to file-object fails.
"""
if display_type == "MIR":
return _get_screenshot_mir()
elif display_type == "X11":
return _get_screenshot_x11()
else:
raise RuntimeError(
"Don't know how to take screen shot for this display server: {}"
.format(display_type)
)
def _get_screenshot_x11():
"""Capture screenshot from an X11 display.
:raises RuntimeError: If saving pixbuf to fileobject fails.
"""
pixbuf_data = _get_x11_pixbuf_data()
return _save_gdk_pixbuf_to_fileobject(pixbuf_data)
def _get_x11_pixbuf_data():
Gdk = autopilot._glib._import_gdk()
window = Gdk.get_default_root_window()
x, y, width, height = window.get_geometry()
return Gdk.pixbuf_get_from_window(window, x, y, width, height)
def _save_gdk_pixbuf_to_fileobject(pixbuf):
image_data = pixbuf.save_to_bufferv("png", [], [])
if image_data[0] is True:
image_datafile = BytesIO()
image_datafile.write(image_data[1])
image_datafile.seek(0)
return image_datafile
logger.error("Unable to write image data.")
raise RuntimeError("Failed to save image data to file object.")
def _get_screenshot_mir():
"""Capture screenshot from Mir display.
:raises FileNotFoundError: If the mirscreencast utility is not found.
:raises CalledProcessError: If the mirscreencast utility errors while
taking a screenshot.
:raises ValueError: If the PNG conversion step fails.
"""
from autopilot.display import Display
display_resolution = Display.create().get_screen_geometry(0)[2:]
screenshot_filepath = _take_mirscreencast_screenshot()
try:
png_data_file = _get_png_from_rgba_file(
screenshot_filepath,
display_resolution
)
finally:
os.remove(screenshot_filepath)
return png_data_file
def _take_mirscreencast_screenshot():
"""Takes a single frame capture of the screen using mirscreencast.
Return the path to the resulting rgba file.
:raises FileNotFoundError: If the mirscreencast utility is not found.
:raises CalledProcessError: If the mirscreencast utility errors while
taking a screenshot.
"""
timestamp = int(time.time())
filename = "ap-screenshot-data-{ts}.rgba".format(ts=timestamp)
filepath = os.path.join(tempfile.gettempdir(), filename)
try:
subprocess.check_call([
"mirscreencast",
"-m", "/run/mir_socket",
"-n", "1",
"-f", filepath
])
except FileNotFoundError as e:
e.args += ("The utility 'mirscreencast' is not available.", )
raise
except subprocess.CalledProcessError as e:
e.args += ("Failed to take screenshot.", )
raise
return filepath
# This currently uses PIL but I'm investigating using something
# quicker/lighter-weight.
def _get_png_from_rgba_file(filepath, image_size):
"""Convert an rgba file to a png file stored in a filelike object.
Returns a BytesIO object containing the png data.
"""
image_data = _image_data_from_file(filepath, image_size)
bio = BytesIO()
image_data.save(bio, format="png")
bio.seek(0)
return bio
def _image_data_from_file(filepath, image_size):
with open(filepath, "rb") as f:
image_data = Image.frombuffer(
"RGBA", image_size, f.read(), "raw", "RGBA", 0, 1
)
return image_data
|