def scale_bitmap(self, img: wx.Image): def get_new_size(_ow, _oh, _w, _h, on_w): if on_w: _nw = _ow - self.padding * 2 _nh = _nw * _h / _w else: _nh = _oh - self.padding * 2 _nw = _nh * _w / _h return _nw, _nh ow, oh = self.GetSize() w, h = img.GetSize() nw, nh = img.GetSize() if w > h: # 横向图 if w > ow: nw, nh = get_new_size(ow, oh, w, h, True) if nh > oh: nw, nh = get_new_size(ow, oh, w, h, False) elif h > oh: nw, nh = get_new_size(ow, oh, w, h, False) if nw > ow: nw, nh = get_new_size(ow, oh, w, h, True) else: # 纵向图 if h > oh: nw, nh = get_new_size(ow, oh, w, h, False) if nw > ow: nw, nh = get_new_size(ow, oh, w, h, True) elif w > ow: nw, nh = get_new_size(ow, oh, w, h, True) if nh > oh: nw, nh = get_new_size(ow, oh, w, h, False) img = img.Scale(nw, nh, wx.IMAGE_QUALITY_HIGH) bitmap = wx.BitmapFromImage(img) return bitmap
def rescale_image(image: wx.Image, max_width: int, max_height: int): """ Rescale a wx.Image and return a wx.Bitmap. """ width, height = image.GetSize() ratio = width / height new_width = int(ratio * max_height) new_height = int(max_width / ratio) if new_width <= max_width: image.Rescale(new_width, max_height, wx.IMAGE_QUALITY_HIGH) else: image.Rescale(max_width, new_height, wx.IMAGE_QUALITY_HIGH) return wx.Bitmap(image)
def _get_ratio(image: wx.Image): size = image.GetSize() width_height = size[0] / size[1] return width_height