Performs central crops of the given image list.

  Args:
    image: a 3-D image tensor
    crop_height: the height of the image following the crop.
    crop_width: the width of the image following the crop.

  Returns:
    3-D tensor with cropped image.
970409
def _central_crop(image, crop_height, crop_width):
  
  shape = tf.shape(image)
  height, width = shape[0], shape[1]

  mlperf_log.resnet_print(key=mlperf_log.INPUT_CENTRAL_CROP,
                          value=[crop_height, crop_width])

  amount_to_be_cropped_h = (height - crop_height)
  crop_top = amount_to_be_cropped_h // 2
  amount_to_be_cropped_w = (width - crop_width)
  crop_left = amount_to_be_cropped_w // 2
  return tf.slice(
      image, [crop_top, crop_left, 0], [crop_height, crop_width, -1])
236819
def crop_keypoint_by_coords(keypoint, crop_coords, crop_height, crop_width, rows, cols):
    
    x, y, a, s = keypoint
    x1, y1, x2, y2 = crop_coords
    cropped_keypoint = [x - x1, y - y1, a, s]
    return cropped_keypoint
446059
def create_crop(self, name, file_obj,
                    x=None, x2=None, y=None, y2=None):
        

        if name not in self._registry:
            return

        file_obj.seek(0)
        im = Image.open(file_obj)
        config = self._registry[name]

        if x is not None and x2 and y is not None and y2 and not config.editable:
            # You can't ask for something special
            # for non editable images
            return

        im = config.rotate_by_exif(im)
        crop_spec = config.get_crop_spec(im, x=x, x2=x2, y=y, y2=y2)
        image = config.process_image(im, crop_spec=crop_spec)
        if image:
            crop_name = utils.get_size_filename(file_obj.name, name)
            self._save_file(image, crop_name)
            return crop_spec
637241
def resize_lazy(image, width=None, height=None, crop=False, force=False,
                namespace="resized", storage=default_storage,
                as_url=False):
    
    # First normalize params to determine which file to get
    width, height, crop = _normalize_params(image, width, height, crop)
    # Fetch the name of the resized image so i can test it if exists
    name = _get_resized_name(image, width, height, crop, namespace)

    # Fetch storage if an image has a specific storage
    try:
        storage = image.storage
    except AttributeError:
        pass

    # Test if exists or force
    if force or not storage.exists(name):
        resized_image = None
        try:
            resized_image = resize(image, width, height, crop)
            name = storage.save(name, resized_image)
        finally:
            if resized_image is not None:
                resized_image.close()

    if as_url:
        return storage.url(name)

    return name
682530
def get_rendition_size(self, spec, output_scale, crop):
        

        if crop:
            # Use the cropping rectangle size
            _, _, width, height = crop
        else:
            # Use the original image size
            width = self._record.width
            height = self._record.height

        mode = spec.get('resize', 'fit')
        if mode == 'fit':
            return self.get_rendition_fit_size(spec, width, height, output_scale)

        if mode == 'fill':
            return self.get_rendition_fill_size(spec, width, height, output_scale)

        if mode == 'stretch':
            return self.get_rendition_stretch_size(spec, width, height, output_scale)

        raise ValueError("Unknown resize mode {}".format(mode))
509935
def _random_crop(image, size):
  """"""
  bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
  random_image, bbox = distorted_bounding_box_crop(
      image,
      bbox,
      min_object_covered=0.1,
      aspect_ratio_range=(3. / 4, 4. / 3.),
      area_range=(0.08, 1.0),
      max_attempts=1,
      scope=None)
  bad = _at_least_x_are_true(tf.shape(image), tf.shape(random_image), 3)

  image = tf.cond(
      bad, lambda: _center_crop(_do_scale(image, size), size),
      lambda: tf.image.resize_bicubic([random_image], [size, size])[0])
  return image
144626
def smart_crop(crop_w, crop_h, image_path):
    
    cropping = face_and_energy_detector(image_path)
    img = Image.open(image_path)
    w, h = img.size
    scaled_size = get_crop_size(crop_w, crop_h, *img.size)
    gravity_x = int(round(scaled_size[0] * cropping.gravity[0]))
    gravity_y = int(round(scaled_size[1] * cropping.gravity[1]))
    if scaled_size[0] == crop_w:
        # find the top and bottom crops
        crop_top, crop_bot = calc_subrange(scaled_size[1], crop_h, gravity_y)
        return scaled_size, Rect(left=0, top=crop_top, right=crop_w, bottom=crop_bot)
    else:
        # find the right and left crops
        crop_left, crop_right = calc_subrange(scaled_size[0], crop_w, gravity_x)
        return scaled_size, Rect(left=crop_left, top=0, right=crop_right, bottom=crop_h)
1226159
def crop_box(endpoint=None, filename=None):
        
        crop_size = current_app.config['AVATARS_CROP_BASE_WIDTH']

        if endpoint is None or filename is None:
            url = url_for('avatars.static', filename='default/default_l.jpg')
        else:
            url = url_for(endpoint, filename=filename)
        return Markup('<img src="%s" id="crop-box" style="max-width: %dpx; display: block;">' % (url, crop_size))
379879
public function resizeImage(Image $image, int $width, int $height, bool $crop = false): Image
    {
        if (true === $image->getEncrypted()) {
            $this->decryptImage($image);
            $encrypt = true;
        } else {
            $encrypt = false;
        }

        $dimensions = $this->getAdjustedImageDimensions($image, $width, $height, $crop);
        $tmp = new Image($this->secret, null, $image->getExtension(), $this->autorotate);
        $tmp->create($dimensions['width'], $dimensions['height']);
        imagecopyresampled(
            $tmp->getData(),
            $image->getData(),
            0,
            0,
            0,
            0,
            $dimensions['width'],
            $dimensions['height'],
            $image->getWidth(),
            $image->getHeight()
        );
        $image->destroy();
        $image = $tmp;

        if (true === $crop) {
            $image = $this->cropImage($image, $width, $height);
        }

        if (true === $encrypt) {
            $image = $this->encryptImage($image);
        }
        return $image;
    }
664866
def display(self, image):
        
        assert(image.size == self.size)

        self.image = self.preprocess(image).copy()
77899
def image2np(image:Tensor)->np.ndarray:
    ""
    res = image.cpu().permute(1,2,0).numpy()
    return res[...,0] if res.shape[2]==1 else res
1121050
def create(self, image, geometry, options):
        
        image = self.cropbox(image, geometry, options)
        image = self.orientation(image, geometry, options)
        image = self.colorspace(image, geometry, options)
        image = self.remove_border(image, options)
        image = self.scale(image, geometry, options)
        image = self.crop(image, geometry, options)
        image = self.rounded(image, geometry, options)
        image = self.blur(image, geometry, options)
        image = self.padding(image, geometry, options)
        return image
826591
def scale(self, width: int, height: int) -> None:
        
        lib.TCOD_image_scale(self.image_c, width, height)
        self.width, self.height = width, height
336206
def parse_size(image, size):
        
        bits = size.split("x")
        if image.size[0] == 0 or image.size[1] == 0:
            ratio = 1.0
        else:
            ratio = float(image.size[0]) / float(image.size[1])

        if len(bits) == 1 or not bits[1]:
            width = int(bits[0])
            height = int(1 / ratio * width)
        elif not bits[0]:
            height = int(bits[1])
            width = int(height * ratio)
        else:
            width, height = map(int, bits)
        return width, height
465876
def _do_crop(self, lines, width, height, x_crop, y_crop):
        '''
        '''
        lines = crop_or_expand(
            lines, height, default=[self.fillchar * width],
            scheme=self._schemes[y_crop])
        for i, line in enumerate(lines):
            lines[i] = crop_or_expand(
                line, width, default=self.fillchar,
                scheme=self._schemes[x_crop])
        return lines
595206
def crop(im, r, c, sz_h, sz_w):
    '''
    
    '''
    return im[r:r+sz_h, c:c+sz_w]
453933
def get_crop_spec(self, im, x=None, x2=None, y=None, y2=None):
        
        w, h = [float(v) for v in im.size]
        upscale = self.upscale
        if x is not None and x2 and y is not None and y2:
            upscale = True
            w = float(x2)-x
            h = float(y2)-y
        else:
            x = 0
            x2 = w
            y = 0
            y2 = h

        if self.width and self.height:
            ry = self.height / h
            rx = self.width / w
            if rx < ry:
                ratio = ry
                adjust = self._adjust_coordinates(ratio, w, self.width)
                x = x + adjust
                x2 = x2 - adjust
            else:
                ratio = rx
                adjust = self._adjust_coordinates(ratio, h, self.height)
                y = y + adjust
                y2 = y2 - adjust

            width = self.width
            height = self.height
        elif self.width:
            ratio = self.width / w
            width = self.width
            height = int(h * ratio)
        else:
            ratio = self.height / h
            width = int(w * ratio)
            height = self.height

        if ratio > 1 and not upscale:
            return

        x, x2, y, y2 = int(x), int(x2), int(y), int(y2)
        return CropSpec(name=self.name,
                        editable=self.editable,
                        width=width, height=height,
                        x=x, x2=x2, y=y, y2=y2)