Calculate dimension of an image during scaling with aspect ratio
435303
def scale_to(x, ratio, targ): 
    ''''''
    return max(math.floor(x*ratio), targ)
191241
def resize_bitmap(parent, _bitmap, target_height):
  '''
  
  '''
  image = wx.ImageFromBitmap(_bitmap)
  _width, _height = image.GetSize()
  if _height < target_height:
    return wx.StaticBitmap(parent, -1, wx.BitmapFromImage(image))
  ratio = float(_width) / _height
  image = image.Scale(target_height * ratio, target_height, wx.IMAGE_QUALITY_HIGH)
  return wx.StaticBitmap(parent, -1, wx.BitmapFromImage(image))
175551
def _imshow_array2d(self, array, origin='lower', interpolation='none',
                        aspect='auto', **kwargs):
        
        # NOTE: If you change the defaults for this method, please update
        #       the docstring for `imshow` above.

        # calculate extent
        extent = tuple(array.xspan) + tuple(array.yspan)
        if self.get_xscale() == 'log' and extent[0] == 0.:
            extent = (1e-300,) + extent[1:]
        if self.get_yscale() == 'log' and extent[2] == 0.:
            extent = extent[:2] + (1e-300,) + extent[3:]
        kwargs.setdefault('extent', extent)

        return self.imshow(array.value.T, origin=origin, aspect=aspect,
                           interpolation=interpolation, **kwargs)
439982
def _resize(self, ratio_x, ratio_y, resampling):
        """"""
        new_width = int(np.ceil(self.width * ratio_x))
        new_height = int(np.ceil(self.height * ratio_y))

        dest_affine = self.affine * Affine.scale(1 / ratio_x, 1 / ratio_y)
        if self.not_loaded():
            window = rasterio.windows.Window(0, 0, self.width, self.height)
            resized_raster = self.get_window(window, xsize=new_width, ysize=new_height, resampling=resampling)
        else:
            resized_raster = self._reproject(new_width, new_height, dest_affine, resampling=resampling)
        return resized_raster
961867
private RenditionMetadata getVirtualRendition(RenditionMetadata rendition, long widthValue, long heightValue, double ratioValue) {

    long width = widthValue;
    long height = heightValue;
    double ratio = ratioValue;

    // if ratio is missing: calculate from given rendition
    if (ratio < MediaFormatHandler.RATIO_TOLERANCE) {
      ratio = (double)rendition.getWidth() / (double)rendition.getHeight();
    }

    // if height is missing - calculate from width
    if (height == 0 && width > 0) {
      height = (int)Math.round(width / ratio);
    }

    // if width is missing - calculate from height
    if (width == 0 && height > 0) {
      width = (int)Math.round(height * ratio);
    }

    // return virtual rendition
    if (width > 0 && height > 0) {
      if (rendition instanceof VirtualTransformedRenditionMetadata) {
        VirtualTransformedRenditionMetadata cropRendition = (VirtualTransformedRenditionMetadata)rendition;
        return new VirtualTransformedRenditionMetadata(cropRendition.getRendition(), width, height,
            cropRendition.getCropDimension(), cropRendition.getRotation());
      }
      else {
        return new VirtualRenditionMetadata(rendition.getRendition(), width, height);
      }
    }
    else {
      return null;
    }
  }
1360304
public static Image scale(Image srcImage, int width, int height, Color fixedColor) {
		return Img.from(srcImage).scale(width, height, fixedColor).getImg();
	}
534083
def to_rectangular(image):
    
    # xy image, vertical scaled to 360 degrees
    xy = pyvips.Image.xyz(image.width, image.height)
    xy *= [1, 360.0 / image.height]

    index = xy.rect()

    # scale to image rect
    scale = min(image.width, image.height) / float(image.width)
    index *= scale / 2.0
    index += [image.width / 2.0, image.height / 2.0]

    return image.mapim(index)
529752
function calculateCanvasSize() {
    var width = view.getCanvasWidth();
    var _scale = docSize.x / width;
    var height = docSize.y / _scale;

    view.setCanvasHeight(height);

    canvasSize.x = width;
    canvasSize.y = height;
  }
160918
def calc_resize_factor(prediction, image_size):
    
    resize_factor_x = prediction.shape[1] / float(image_size[1])
    resize_factor_y = prediction.shape[0] / float(image_size[0])
    if abs(resize_factor_x - resize_factor_y) > 1.0/image_size[1] :
        raise RuntimeError("""The aspect ratio of the fixations does not
                              match with the prediction: %f vs. %f"""
                              %(resize_factor_y, resize_factor_x))
    return (resize_factor_y, resize_factor_x)
660478
def get_rendition_size(self, width=0, height=0):
        ''''''
        if width == 0 and height == 0:
            return (self.master_width, self.master_height)

        target_width = int(width)
        target_height = int(height)

        ratio = self.master_width / float(self.master_height)
        if target_height == 0 and target_width != 0:
            target_height = int(target_width / ratio)

        if target_height != 0 and target_width == 0:
            target_width = int(target_height * ratio)

        return target_width, target_height
437593
def resize_compute_width_height(fullfile,_megapixels):
    """"""

    img = Image.open(fullfile)
    width,height=img.size

    current_megapixels=width*height/(2.0**20)
    scale=sqrt(_megapixels/float(current_megapixels))

    logger.debug('A resize scale would be %f'%(scale))
    # Can't make bigger, return original
    if scale>= 1.0:
        logger.warning('Image is %0.1f MP, trying to scale to %0.1f MP - just using original!',
                current_megapixels,_megapixels);
        return width,height

    new_width=int(width*scale)
    new_height=int(height*scale)

    return new_width,new_height
7160
def __handle_scale_rot(self):
        """"""
        if self.__is_rot_pending:
            self.__execute_rot(self.untransformed_image)
            self.__is_rot_pending = False

            # Scale the image using the recently rotated surface to keep the orientation correct
            self.__execute_scale(self.image, self.image.get_size())
            self.__is_scale_pending = False

        # The image is not rotating while scaling, thus use the untransformed image to scale.
        if self.__is_scale_pending:
            self.__execute_scale(self.untransformed_image, self.untransformed_image.get_size())
            self.__is_scale_pending = False
45984
def resize(img, width, height):
    
    return img.resize((width, height), Image.ANTIALIAS)
17961
private static int getScaleFactor(ImageMetadata metadata, int maxPixels) {
        int scale = 1;
        int scaledW = metadata.getW();
        int scaledH = metadata.getH();
        while (scaledW * scaledH > maxPixels) {
            scale *= 2;
            scaledH /= 2;
            scaledW /= 2;
        }
        return scale;
    }
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
1026784
def image_set_aspect(aspect=1.0, axes="gca"):
    
    if axes is "gca": axes = _pylab.gca()

    e = axes.get_images()[0].get_extent()
    axes.set_aspect(abs((e[1]-e[0])/(e[3]-e[2]))/aspect)
225453
function getImageNewAspectSizes(img, max_width, max_height) {
    var img_width = this.getImageWidth(img);
    var img_height = this.getImageHeight(img);
    if (img_width === 0 || img_height === 0) {
      throw new Error('Image width or height is 0 in BunnyImage.getImageNewAspectSizes().');
    }
    var ratio = Math.min(max_width / img_width, max_height / img_height);

    return {
      width: Math.floor(img_width * ratio),
      height: Math.floor(img_height * ratio)
    };
  }