thirdparty/google_appengine/google/appengine/api/images/images_stub.py
changeset 1278 a7766286a7be
parent 686 df109be0567c
child 2273 e4cb9c53db3e
equal deleted inserted replaced
1277:5c931bd3dc1e 1278:a7766286a7be
    20 
    20 
    21 
    21 
    22 import logging
    22 import logging
    23 import StringIO
    23 import StringIO
    24 
    24 
    25 import PIL
    25 try:
    26 from PIL import _imaging
    26   import PIL
    27 from PIL import Image
    27   from PIL import _imaging
       
    28   from PIL import Image
       
    29 except ImportError:
       
    30   import _imaging
       
    31   import Image
    28 
    32 
    29 from google.appengine.api import apiproxy_stub
    33 from google.appengine.api import apiproxy_stub
    30 from google.appengine.api import images
    34 from google.appengine.api import images
    31 from google.appengine.api.images import images_service_pb
    35 from google.appengine.api.images import images_service_pb
    32 from google.appengine.runtime import apiproxy_errors
    36 from google.appengine.runtime import apiproxy_errors
   244            int(transform.crop_right_x() * width),
   248            int(transform.crop_right_x() * width),
   245            int(transform.crop_bottom_y() * height))
   249            int(transform.crop_bottom_y() * height))
   246 
   250 
   247     return image.crop(box)
   251     return image.crop(box)
   248 
   252 
   249   def _CheckTransformCount(self, transform_map, req_transform):
   253   def _ProcessTransforms(self, image, transforms):
   250     """Check that the requested transform hasn't already been set in map.
   254     """Execute PIL operations based on transform values.
   251 
   255 
   252     Args:
   256     Args:
   253       transform_map: {images_service_pb.ImagesServiceTransform: boolean}, map
   257       image: PIL.Image.Image instance, image to manipulate.
   254         to use to determine if the requested transform has been called.
   258       trasnforms: list of ImagesTransformRequest.Transform objects.
   255       req_transform: images_service_pb.ImagesServiceTransform, the requested
   259 
   256         transform.
   260     Returns:
       
   261       PIL.Image.Image with transforms performed on it.
   257 
   262 
   258     Raises:
   263     Raises:
   259       BadRequestError if we are passed more than one of the same type of
   264       BadRequestError if we are passed more than one of the same type of
   260       transform.
   265       transform.
   261     """
   266     """
   262     if req_transform in transform_map:
       
   263       raise apiproxy_errors.ApplicationError(
       
   264           images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA)
       
   265     transform_map[req_transform] = True
       
   266 
       
   267   def _ProcessTransforms(self, image, transforms):
       
   268     """Execute PIL operations based on transform values.
       
   269 
       
   270     Args:
       
   271       image: PIL.Image.Image instance, image to manipulate.
       
   272       trasnforms: list of ImagesTransformRequest.Transform objects.
       
   273 
       
   274     Returns:
       
   275       PIL.Image.Image with transforms performed on it.
       
   276 
       
   277     Raises:
       
   278       BadRequestError if we are passed more than one of the same type of
       
   279       transform.
       
   280     """
       
   281     new_image = image
   267     new_image = image
   282     transform_map = {}
   268     if len(transforms) > images.MAX_TRANSFORMS_PER_REQUEST:
       
   269       raise apiproxy_errors.ApplicationError(
       
   270           images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA)
   283     for transform in transforms:
   271     for transform in transforms:
   284       if transform.has_width() or transform.has_height():
   272       if transform.has_width() or transform.has_height():
   285         self._CheckTransformCount(
       
   286             transform_map,
       
   287             images_service_pb.ImagesServiceTransform.RESIZE
       
   288         )
       
   289 
       
   290         new_image = self._Resize(new_image, transform)
   273         new_image = self._Resize(new_image, transform)
   291 
   274 
   292       elif transform.has_rotate():
   275       elif transform.has_rotate():
   293         self._CheckTransformCount(
       
   294             transform_map,
       
   295             images_service_pb.ImagesServiceTransform.ROTATE
       
   296         )
       
   297 
       
   298         new_image = self._Rotate(new_image, transform)
   276         new_image = self._Rotate(new_image, transform)
   299 
   277 
   300       elif transform.has_horizontal_flip():
   278       elif transform.has_horizontal_flip():
   301         self._CheckTransformCount(
       
   302             transform_map,
       
   303             images_service_pb.ImagesServiceTransform.HORIZONTAL_FLIP
       
   304         )
       
   305 
       
   306         new_image = new_image.transpose(Image.FLIP_LEFT_RIGHT)
   279         new_image = new_image.transpose(Image.FLIP_LEFT_RIGHT)
   307 
   280 
   308       elif transform.has_vertical_flip():
   281       elif transform.has_vertical_flip():
   309         self._CheckTransformCount(
       
   310             transform_map,
       
   311             images_service_pb.ImagesServiceTransform.VERTICAL_FLIP
       
   312         )
       
   313 
       
   314         new_image = new_image.transpose(Image.FLIP_TOP_BOTTOM)
   282         new_image = new_image.transpose(Image.FLIP_TOP_BOTTOM)
   315 
   283 
   316       elif (transform.has_crop_left_x() or
   284       elif (transform.has_crop_left_x() or
   317           transform.has_crop_top_y() or
   285           transform.has_crop_top_y() or
   318           transform.has_crop_right_x() or
   286           transform.has_crop_right_x() or
   319           transform.has_crop_bottom_y()):
   287           transform.has_crop_bottom_y()):
   320         self._CheckTransformCount(
       
   321             transform_map,
       
   322             images_service_pb.ImagesServiceTransform.CROP
       
   323         )
       
   324 
       
   325         new_image = self._Crop(new_image, transform)
   288         new_image = self._Crop(new_image, transform)
   326 
   289 
   327       elif transform.has_autolevels():
   290       elif transform.has_autolevels():
   328         self._CheckTransformCount(
       
   329             transform_map,
       
   330             images_service_pb.ImagesServiceTransform.IM_FEELING_LUCKY
       
   331         )
       
   332         logging.info("I'm Feeling Lucky autolevels will be visible once this "
   291         logging.info("I'm Feeling Lucky autolevels will be visible once this "
   333                      "application is deployed.")
   292                      "application is deployed.")
   334       else:
   293       else:
   335         logging.warn("Found no transformations found to perform.")
   294         logging.warn("Found no transformations found to perform.")
   336 
   295