Determine the scale-crop size given the provided spec
15343
def get_rendition_fill_size(spec, input_w, input_h, output_scale):
        

        width = input_w
        height = input_h

        scale = spec.get('scale')
        if scale:
            width = width / scale
            height = height / scale

        if spec.get('scale_min_width'):
            width = max(width, spec['spec_min_width'])

        if spec.get('scale_min_height'):
            height = max(height, spec['scale_min_height'])

        if spec.get('width'):
            width = min(width, spec['width'])
        if spec.get('max_width'):
            width = min(width, spec['max_width'])

        if spec.get('height'):
            height = min(height, spec['height'])
        if spec.get('max_height'):
            height = min(height, spec['max_height'])

        width = width * output_scale
        height = height * output_scale

        # Never scale to larger than the base rendition (but keep the output
        # aspect)
        if width > input_w:
            height = height * input_w / width
            width = input_w

        if height > input_h:
            width = width * input_h / height
            height = input_h

        # Determine the box size
        box_w = min(input_w, round(width * input_h / height))
        box_h = min(input_h, round(height * input_w / width))

        # Box offset
        box_x = round((input_w - box_w) * spec.get('fill_crop_x', 0.5))
        box_y = round((input_h - box_h) * spec.get('fill_crop_y', 0.5))

        return (round(width), round(height)), (box_x, box_y, box_x + box_w, box_y + box_h)
1197645
function(size, callback) {
      var ratio = size.width / size.height;
      var cropPosition = {};
      var resizeWidth = width || Math.round(height * ratio);
      var resizeHeight = height || Math.round(width / ratio);

      if (crop && width && height) {
        if (ratio < width / height) {
          resizeHeight = Math.round(width / ratio);
          cropPosition = {x: 0, y: Math.round((resizeHeight - height) / 2)};
          crop = resizeHeight > height;
        } else {
          resizeWidth = Math.round(height * ratio);
          cropPosition = {x: Math.round((resizeWidth - width) / 2), y: 0};
          crop = resizeWidth > width;
        }
      }

      image
        .noProfile()
        .quality(quality)
        .resizeExact(resizeWidth, resizeHeight);

      if (crop)
        image.crop(width, height, cropPosition.x, cropPosition.y);

      image.write(thumbnailPath, callback);
    }
399560
def _parse_example_spec(self):
    """"""
    height, width = image_util.get_expected_image_size(self.module_spec)
    input_shape = [height, width, 3]
    return {self.key: tf_v1.FixedLenFeature(input_shape, tf.float32)}
10904
def getClosestZoom(self, resolution, unit='meters'):
        
        lo, hi = self._getZoomLevelRange(resolution, unit)
        if lo == 0:
            return lo
        if hi == len(self.RESOLUTIONS):
            return hi - 1
        before = self.RESOLUTIONS[lo - 1]
        if abs(self.RESOLUTIONS[lo] - resolution) < abs(before - resolution):
            return lo
        return lo - 1
994690
private void updateScaling() {

        List<I_CmsTransform> transforms = new ArrayList<>();
        CmsCroppingParamBean crop = m_croppingProvider.get();
        CmsImageInfoBean info = m_imageInfoProvider.get();

        double wv = m_image.getElement().getParentElement().getOffsetWidth();
        double hv = m_image.getElement().getParentElement().getOffsetHeight();
        if (crop == null) {
            transforms.add(
                new CmsBoxFit(CmsBoxFit.Mode.scaleOnlyIfNecessary, wv, hv, info.getWidth(), info.getHeight()));
        } else {
            int wt, ht;
            wt = crop.getTargetWidth() >= 0 ? crop.getTargetWidth() : info.getWidth();
            ht = crop.getTargetHeight() >= 0 ? crop.getTargetHeight() : info.getHeight();
            transforms.add(new CmsBoxFit(CmsBoxFit.Mode.scaleOnlyIfNecessary, wv, hv, wt, ht));
            if (crop.isCropped()) {
                transforms.add(
                    new CmsBoxFit(CmsBoxFit.Mode.scaleAlways, wt, ht, crop.getCropWidth(), crop.getCropHeight()));
                transforms.add(new CmsTranslate(crop.getCropX(), crop.getCropY()));
            } else {
                transforms.add(
                    new CmsBoxFit(CmsBoxFit.Mode.scaleAlways, wt, ht, crop.getOrgWidth(), crop.getOrgHeight()));
            }
        }
        CmsCompositeTransform chain = new CmsCompositeTransform(transforms);
        m_coordinateTransform = chain;
        if ((crop == null) || !crop.isCropped()) {
            m_region = transformRegionBack(
                m_coordinateTransform,
                CmsRectangle.fromLeftTopWidthHeight(0, 0, info.getWidth(), info.getHeight()));
        } else {
            m_region = transformRegionBack(
                m_coordinateTransform,
                CmsRectangle.fromLeftTopWidthHeight(
                    crop.getCropX(),
                    crop.getCropY(),
                    crop.getCropWidth(),
                    crop.getCropHeight()));
        }
    }
1035600
def pixel_width(self):
        
        return self.zoom_factor * ((self._finish - self._start) / self._resolution)
476454
def crop(self, image, size, crop, options):
        
        if not crop:
            return image
        return self.engine_crop(image, size, crop, options)
794002
def crop_sizes(cls, original_width, original_height, target_width, target_height):
        
        if target_width < original_width:
            left = abs(original_width - target_width) / 2
            right = left + target_width
        else:
            left = 0
            right = original_width
        if target_height < original_height:
            upper = abs(original_height - target_height) / 2
            lower = upper + target_height
        else:
            upper = 0
            lower = original_height

        return left, upper, right, lower
1020742
def center_crop(dimensions, upsample)
      if is_smaller_than(dimensions) && upsample == false
        dimensions = reduce_to_image(dimensions)
      end
      image_file.thumb("#{dimensions_to_string(dimensions)}#")
    end
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])
258401
def calculate_origin_and_size(canvas_size, data_shape, image_canvas_mode, image_zoom, image_position) -> typing.Tuple[typing.Any, typing.Any]:
    """"""
    if data_shape is None:
        return None, None
    if image_canvas_mode == "fill":
        data_shape = data_shape
        scale_h = float(data_shape[1]) / canvas_size[1]
        scale_v = float(data_shape[0]) / canvas_size[0]
        if scale_v < scale_h:
            image_canvas_size = (canvas_size[0], canvas_size[0] * data_shape[1] / data_shape[0])
        else:
            image_canvas_size = (canvas_size[1] * data_shape[0] / data_shape[1], canvas_size[1])
        image_canvas_origin = (canvas_size[0] * 0.5 - image_canvas_size[0] * 0.5, canvas_size[1] * 0.5 - image_canvas_size[1] * 0.5)
    elif image_canvas_mode == "fit":
        image_canvas_size = canvas_size
        image_canvas_origin = (0, 0)
    elif image_canvas_mode == "1:1":
        image_canvas_size = data_shape
        image_canvas_origin = (canvas_size[0] * 0.5 - image_canvas_size[0] * 0.5, canvas_size[1] * 0.5 - image_canvas_size[1] * 0.5)
    elif image_canvas_mode == "2:1":
        image_canvas_size = (data_shape[0] * 0.5, data_shape[1] * 0.5)
        image_canvas_origin = (canvas_size[0] * 0.5 - image_canvas_size[0] * 0.5, canvas_size[1] * 0.5 - image_canvas_size[1] * 0.5)
    else:
        image_canvas_size = (canvas_size[0] * image_zoom, canvas_size[1] * image_zoom)
        canvas_rect = Geometry.fit_to_size(((0, 0), image_canvas_size), data_shape)
        image_canvas_origin_y = (canvas_size[0] * 0.5) - image_position[0] * canvas_rect[1][0] - canvas_rect[0][0]
        image_canvas_origin_x = (canvas_size[1] * 0.5) - image_position[1] * canvas_rect[1][1] - canvas_rect[0][1]
        image_canvas_origin = (image_canvas_origin_y, image_canvas_origin_x)
    return image_canvas_origin, image_canvas_size
366282
def _get_resize_target(img, crop_target, do_crop=False)->TensorImageSize:
    ""
    if crop_target is None: return None
    ch,r,c = img.shape
    target_r,target_c = crop_target
    ratio = (min if do_crop else max)(r/target_r, c/target_c)
    return ch,int(round(r/ratio)),int(round(c/ratio))
1106562
private function getCropPlacing($optimalWidth, $optimalHeight, $newWidth, $newHeight, $pos = 'm')
		#
		# Author:   Jarrod Oberto
		# Date:   July 11
		# Purpose:  Set the cropping area.
		# Params in:
		# Params out: (array) the crop x and y co-ordinates.
		# Notes:    When specifying the exact pixel crop position (eg 10x15), be
		#       very careful as it's easy to crop out of the image leaving
		#       black borders.
		#
	{
		$pos = fix_strtolower($pos);

		// *** If co-ords have been entered
		if (strstr($pos, 'x'))
		{
			$pos = str_replace(' ', '', $pos);

			$xyArray = explode('x', $pos);
			list($cropStartX, $cropStartY) = $xyArray;

		}
		else
		{

			switch ($pos)
			{
				case 'tl':
					$cropStartX = 0;
					$cropStartY = 0;
					break;

				case 't':
					$cropStartX = ($optimalWidth / 2) - ($newWidth / 2);
					$cropStartY = 0;
					break;

				case 'tr':
					$cropStartX = $optimalWidth - $newWidth;
					$cropStartY = 0;
					break;

				case 'l':
					$cropStartX = 0;
					$cropStartY = ($optimalHeight / 2) - ($newHeight / 2);
					break;

				case 'm':
					$cropStartX = ($optimalWidth / 2) - ($newWidth / 2);
					$cropStartY = ($optimalHeight / 2) - ($newHeight / 2);
					break;

				case 'r':
					$cropStartX = $optimalWidth - $newWidth;
					$cropStartY = ($optimalHeight / 2) - ($newHeight / 2);
					break;

				case 'bl':
					$cropStartX = 0;
					$cropStartY = $optimalHeight - $newHeight;
					break;

				case 'b':
					$cropStartX = ($optimalWidth / 2) - ($newWidth / 2);
					$cropStartY = $optimalHeight - $newHeight;
					break;

				case 'br':
					$cropStartX = $optimalWidth - $newWidth;
					$cropStartY = $optimalHeight - $newHeight;
					break;

				case 'auto':
					// *** If image is a portrait crop from top, not center. v1.5
					if ($optimalHeight > $optimalWidth)
					{
						$cropStartX = ($optimalWidth / 2) - ($newWidth / 2);
						$cropStartY = ($this->cropFromTopPercent / 100) * $optimalHeight;
					}
					else
					{

						// *** Else crop from the center
						$cropStartX = ($optimalWidth / 2) - ($newWidth / 2);
						$cropStartY = ($optimalHeight / 2) - ($newHeight / 2);
					}
					break;

				default:
					// *** Default to center
					$cropStartX = ($optimalWidth / 2) - ($newWidth / 2);
					$cropStartY = ($optimalHeight / 2) - ($newHeight / 2);
					break;
			}
		}

		return array( 'x' => $cropStartX, 'y' => $cropStartY );
	}
443343
def crop_resized(image, size, gravity = "Center")
      size =~ /(\d+)x(\d+)/
      width = $1.to_i
      height = $2.to_i

      # Grab the width and height of the current image in one go.
      cols, rows = image[:dimensions]

      # Only do anything if needs be. Who knows, maybe it's already the exact
      # dimensions we're looking for.
      if(width != cols && height != rows)
        image.combine_options do |c|
          # Scale the image down to the widest dimension.
          if(width != cols || height != rows)
            scale = [width / cols.to_f, height / rows.to_f].max * 100
            c.resize("#{scale}%")
          end

          # Align how things will be cropped.
          c.gravity(gravity)

          # Crop the image to size.
          c.crop("#{width}x#{height}+0+0")
        end
      end
    end
1152179
def crop(im, r, c, sz): 
    '''
     
    '''
    return im[r:r+sz, c:c+sz]
918789
def size_when_fitting(target, dimensions = get_base_dimensions)
      zoom = [
        dimensions[:width].to_f / target[:width],
        dimensions[:height].to_f / target[:height]
      ].max

      if zoom == 0.0
        width = target[:width]
        height = target[:height]
      else
        width = (dimensions[:width] / zoom).round
        height = (dimensions[:height] / zoom).round
      end

      {width: width.to_i, height: height.to_i}
    end
68144
def scale_and_crop_with_ranges(
    im, size, size_range=None, crop=False, upscale=False, zoom=None, target=None, **kwargs):
    

    min_width, min_height = size

    # if there's no restriction on range, or range isn't given, just act as
    # normal
    if min_width == 0 or min_height == 0 or not size_range:
        return scale_and_crop(im, size, crop, upscale, zoom, target, **kwargs)

    max_width = min_width + size_range[0]
    max_height = min_height + size_range[1]

    # figure out the minimum and maximum aspect ratios
    min_ar = min_width * 1.0 / max_height
    max_ar = max_width * 1.0 / min_height

    img_width, img_height = [float(v) for v in im.size]
    img_ar = img_width/img_height

    # clamp image aspect ratio to given range
    if img_ar <= min_ar:
        size = (min_width, max_height)
    elif img_ar >= max_ar:
        size = (max_width, min_height)
    else:
        # the aspect ratio of the image is within the allowed parameters.
        size = (max_width, max_height)


    return scale_and_crop(im, size, crop, upscale, zoom, target, **kwargs)