def _transform_image(self, blob_key, options): """Construct and execute a transform request using the images stub. Args: blob_key: A str containing the blob_key of the image to transform. options: A str containing the resize and crop options to apply to the image. Returns: A str containing the tranformed (if necessary) image. """ resize, crop = self._parse_options(options) image_data = images_service_pb.ImageData() image_data.set_blob_key(blob_key) image = _get_images_stub()._OpenImageData(image_data) original_mime_type = image.format width, height = image.size # Crop to square if necessary if crop: crop_xform = None if width > height: # landscape: slice the sides crop_xform = images_service_pb.Transform() delta = (width - height) / (width * 2.0) crop_xform.set_crop_left_x(delta) crop_xform.set_crop_right_x(1.0 - delta) elif width < height: # portrait: slice the top and bottom with bias crop_xform = images_service_pb.Transform() delta = (height - width) / (height * 2.0) top_delta = max(0.0, delta - 0.25) bottom_delta = 1.0 - (2.0 * delta) + top_delta crop_xform.set_crop_top_y(top_delta) crop_xform.set_crop_bottom_y(bottom_delta) if crop_xform: image = _get_images_stub()._Crop(image, crop_xform) # Resize if resize is None: if width > _DEFAULT_SERVING_SIZE or height > _DEFAULT_SERVING_SIZE: resize = _DEFAULT_SERVING_SIZE # resize value of 0 is valid and translates to 'serve at original size'. if resize: # Note that resize transform maintains the image aspect ratio. resize_xform = images_service_pb.Transform() resize_xform.set_width(resize) resize_xform.set_height(resize) image = _get_images_stub()._Resize(image, resize_xform) output_settings = images_service_pb.OutputSettings() # EncodeImage only saves out to JPEG or PNG. All image formats other than # GIF or PNG, will be served as a JPEG. output_mime_type = images_service_pb.OutputSettings.JPEG if original_mime_type in ['PNG', 'GIF']: output_mime_type = images_service_pb.OutputSettings.PNG output_settings.set_mime_type(output_mime_type) return (_get_images_stub()._EncodeImage(image, output_settings), _MIME_TYPE_MAP[output_mime_type])
def expect_encode_image(self, data, mime_type=images_service_pb.OutputSettings.JPEG): """Setup a mox expectation to images_stub._EncodeImage.""" output_settings = images_service_pb.OutputSettings() output_settings.set_mime_type(mime_type) self._images_stub._EncodeImage(mox.IsA(MockImage), output_settings).AndReturn(data)
def _TransformImage(self, blob_key, options): """Construct and execute transform request to the images stub. Args: blob_key: blob_key to the image to transform. options: resize and crop option string to apply to the image. Returns: The tranformed (if necessary) image bytes. """ resize, crop = self._ParseOptions(options) image_data = images_service_pb.ImageData() image_data.set_blob_key(blob_key) image = self._images_stub._OpenImageData(image_data) original_mime_type = image.format width, height = image.size if crop: crop_xform = None if width > height: crop_xform = images_service_pb.Transform() delta = (width - height) / (width * 2.0) crop_xform.set_crop_left_x(delta) crop_xform.set_crop_right_x(1.0 - delta) elif width < height: crop_xform = images_service_pb.Transform() delta = (height - width) / (height * 2.0) top_delta = max(0.0, delta - 0.25) bottom_delta = 1.0 - (2.0 * delta) + top_delta crop_xform.set_crop_top_y(top_delta) crop_xform.set_crop_bottom_y(bottom_delta) if crop_xform: image = self._images_stub._Crop(image, crop_xform) if resize is None: if width > DEFAULT_SERVING_SIZE or height > DEFAULT_SERVING_SIZE: resize = DEFAULT_SERVING_SIZE if resize: resize_xform = images_service_pb.Transform() resize_xform.set_width(resize) resize_xform.set_height(resize) image = self._images_stub._Resize(image, resize_xform) output_settings = images_service_pb.OutputSettings() output_mime_type = images_service_pb.OutputSettings.JPEG if original_mime_type in ['PNG', 'GIF']: output_mime_type = images_service_pb.OutputSettings.PNG output_settings.set_mime_type(output_mime_type) return (self._images_stub._EncodeImage(image, output_settings), self._mime_type_map[output_mime_type])