def convert_image(self): if not os.path.exists(self.image_path): return False image = Image(self.image_path, wx.BITMAP_TYPE_PNG) width = image.GetWidth() / 4 height = image.GetHeight() / 4 return image.Scale(width, height).ConvertToBitmap()
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 scale_bitmap(self, image: wx.Image): w = image.GetWidth() h = image.GetHeight() if w > h: ratio = 512 / w else: ratio = 512 / h image = image.Scale(int(w * ratio), int(h * ratio), wx.IMAGE_QUALITY_HIGH) result = wx.BitmapFromImage(image) return result