MCPcopy Create free account
hub / github.com/dtmilano/AndroidViewClient / writeImageToFile

Method writeImageToFile

src/com/dtmilano/android/viewclient.py:1170–1218  ·  view source on GitHub ↗

Write the View image to the specified filename in the specified format. @type filename: str @param filename: Absolute path and optional filename receiving the image. If this points to a directory, then the filename is determined by this View unique ID and fo

(self, filename, _format="PNG")

Source from the content-addressed store, hash-verified

1168 return self.target
1169
1170 def writeImageToFile(self, filename, _format="PNG"):
1171 """
1172 Write the View image to the specified filename in the specified format.
1173
1174 @type filename: str
1175 @param filename: Absolute path and optional filename receiving the image. If this points to
1176 a directory, then the filename is determined by this View unique ID and
1177 format extension.
1178 @type _format: str
1179 @param _format: Image format (default format is PNG)
1180 """
1181
1182 filename = self.device.substituteDeviceTemplate(filename)
1183 if not os.path.isabs(filename):
1184 raise ValueError("writeImageToFile expects an absolute path (fielname='%s')" % filename)
1185 if os.path.isdir(filename):
1186 filename = os.path.join(filename, View.variableNameFromId(self) + '.' + _format.lower())
1187 if DEBUG:
1188 print("writeImageToFile: saving image to '%s' in %s format" % (filename, _format), file=sys.stderr)
1189 # self.device.takeSnapshot().getSubImage(self.getPositionAndSize()).writeToFile(filename, _format)
1190 # crop:
1191 # im.crop(box) ⇒ image
1192 # Returns a copy of a rectangular region from the current image.
1193 # The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.
1194 ((l, t), (r, b)) = self.getCoords()
1195 box = (l, t, r, b)
1196 if DEBUG:
1197 print("writeImageToFile: cropping", box, " reconnect=", self.device.reconnect, file=sys.stderr)
1198 if box == (0, 0, 0, 0):
1199 return
1200 if self.uiAutomatorHelper:
1201 if DEBUG_UI_AUTOMATOR_HELPER:
1202 print("Taking screenshot using UiAutomatorHelper", file=sys.stderr)
1203 received = self.uiAutomatorHelper.ui_device.take_screenshot()
1204 stream = io.BytesIO(received.read())
1205 try:
1206 from PIL import Image
1207 image = Image.open(stream)
1208 except ImportError as ex:
1209 # FIXME: this method should be global
1210 self.pilNotInstalledWarning()
1211 sys.exit(1)
1212 except IOError as ex:
1213 print(ex, file=sys.stderr)
1214 print(repr(stream))
1215 sys.exit(1)
1216 else:
1217 image = self.device.takeSnapshot(reconnect=self.device.reconnect)
1218 image.crop(box).save(filename, _format)
1219
1220 def __smallStr__(self):
1221 # 2to3

Callers 3

saveViewSnapshotMethod · 0.45

Calls 6

getCoordsMethod · 0.95
variableNameFromIdMethod · 0.80
take_screenshotMethod · 0.80
takeSnapshotMethod · 0.45

Tested by

no test coverage detected