def _resize(self, resp): image = Image(resp.buffer, self.settings) opts = self._get_resize_options() resized = image.resize( self.get_argument("w"), self.get_argument("h"), **opts) self._forward_headers(resp.headers) for block in iter(lambda: resized.read(65536), b""): self.write(block) resized.close()
def _pilbox_operation(self, operation): """ Use Pilbox to transform an image """ image = PilboxImage(BytesIO(self.data)) if operation == "region": image.region(self.options.get("rect").split(',')) elif operation == "rotate": image.rotate(deg=self.options.get("deg"), expand=self.options.get("expand")) elif operation == "resize": max_width = self.options.get("max-width") max_height = self.options.get("max-height") resize_width = self.options.get("w") resize_height = self.options.get("h") # Make sure widths and heights are integers if resize_width: resize_width = int(resize_width) if resize_height: resize_height = int(resize_height) if max_width: max_width = int(max_width) if max_height: max_height = int(max_height) # Image size management with WandImage(blob=self.data) as image_info: # Don't allow expanding of images if resize_width or resize_height: width_oversize = resize_width > image_info.width height_oversize = resize_height > image_info.height if (width_oversize or height_oversize): expand_message = ( "Resize error: Maximum dimensions for this image " "are {0}px wide by {1}px high.").format( image_info.width, image_info.height) raise PilboxError(400, log_message=expand_message) # Process max_width and max_height if not resize_width and max_width: if max_width < image_info.width: resize_width = max_width if not resize_height and max_height: if max_height < image_info.height: resize_height = max_height if resize_height or resize_width: image.resize(width=resize_width, height=resize_height, mode=self.options.get("mode"), filter=self.options.get("filter"), background=self.options.get("bg"), retain=self.options.get("retain"), position=self.options.get("pos")) self.data = image.save(quality=self.options.get("q")).read()
def _process_response(self, resp): image = Image(resp.buffer) image.resize(self.w, self.h) return image.save()