def resize_bitmap(b: wx.Bitmap, width, height) -> wx.Bitmap: """resize bitmap so as to fit in specified width and height so as not to exceed the original size. """ # @todo enable to choice Correct the orientation of the image i: wx.Image = b.ConvertToImage() s: wx.Size = i.GetSize() old_w = s.GetWidth() old_h = s.GetHeight() if old_w <= width and old_h <= height: new_w = width new_h = height else: if old_w > width: new_w = width new_h = (old_h * width) / old_w elif old_h > height: new_w = (old_w * height) / old_h new_h = height # Re-correction if overflowing after correction if new_w > width: new_w = width new_h = (old_h * width) / old_w elif new_h > height: new_w = (old_w * height) / old_h new_h = height i = i.Scale(int(new_w), int(new_h), wx.IMAGE_QUALITY_HIGH) return wx.Bitmap(i)
def scale_bitmap(bitmap: wx.Bitmap, width: int, height: int) -> wx.Bitmap: """Rescales a bitmap to a given size. Converting it to image, rescaling and converting it back. """ image = bitmap.ConvertToImage() image = image.Scale(width, height, wx.IMAGE_QUALITY_HIGH) result = wx.Bitmap(image) return result
def RescaleBitmap(bmp: wx.Bitmap, sizeNeeded: wx.Size): """ :param bmp: :type bmp: wx.Bitmap :param sizeNeeded: :type sizeNeeded: wx.Size """ if not sizeNeeded.IsFullySpecified(): raise ValueError("New size must be given") img = bmp.ConvertToImage() img.Rescale(sizeNeeded.x, sizeNeeded.y) bmp = wx.Bitmap(img)
def load_image(self, path, area=None): self.Scroll(0, 0) self.img_path = path self.wx_image = Image(path, BITMAP_TYPE_ANY) width = self.wx_image.GetWidth() height = self.wx_image.GetHeight() if area: x, y, w, h = area bitmap = Bitmap(self.wx_image) bitmap_to_draw = bitmap.GetSubBitmap(Rect(x, y, w, h)) bitmap = bitmap.ConvertToImage().ConvertToGreyscale( 0.156, 0.308, 0.060).ConvertToBitmap() self.original_bitmap = self._get_bitmap(bitmap, bitmap_to_draw, x, y, w, h, False) else: self.original_bitmap = Bitmap(self.wx_image) self.greyscaled_bitmap = self.original_bitmap.ConvertToImage( ).ConvertToGreyscale(0.209, 0.411, 0.080).ConvertToBitmap() self.static_bitmap.SetBitmap(self.original_bitmap) self.SetScrollbars(self.MIN_SCROLL, self.MIN_SCROLL, width / self.MIN_SCROLL, height / self.MIN_SCROLL)
class ImagePanel(ScrolledWindow): MIN_SCROLL = 10 def __init__(self, parent): ScrolledWindow.__init__(self, parent) self.wx_image = None self.original_bitmap = None self.greyscaled_bitmap = None self.img_path = None sizer = BoxSizer(VERTICAL) self.static_bitmap = StaticBitmap(self) sizer.Add(self.static_bitmap, 1, flag=FLAG_ALL_AND_EXPAND) self.SetSizer(sizer) def was_image_loaded(self): return (self.img_path and self.wx_image and self.original_bitmap and self.greyscaled_bitmap) def get_image_dimensions(self): return (self.original_bitmap.GetWidth(), self.original_bitmap.GetHeight()) def load_image(self, path, area=None): self.Scroll(0, 0) self.img_path = path self.wx_image = Image(path, BITMAP_TYPE_ANY) width = self.wx_image.GetWidth() height = self.wx_image.GetHeight() if area: x, y, w, h = area bitmap = Bitmap(self.wx_image) bitmap_to_draw = bitmap.GetSubBitmap(Rect(x, y, w, h)) bitmap = bitmap.ConvertToImage().ConvertToGreyscale( 0.156, 0.308, 0.060).ConvertToBitmap() self.original_bitmap = self._get_bitmap(bitmap, bitmap_to_draw, x, y, w, h, False) else: self.original_bitmap = Bitmap(self.wx_image) self.greyscaled_bitmap = self.original_bitmap.ConvertToImage( ).ConvertToGreyscale(0.209, 0.411, 0.080).ConvertToBitmap() self.static_bitmap.SetBitmap(self.original_bitmap) self.SetScrollbars(self.MIN_SCROLL, self.MIN_SCROLL, width / self.MIN_SCROLL, height / self.MIN_SCROLL) def _get_bitmap(self, bitmap, bitmap_to_draw, x, y, w, h, draw_frame=True): bitmap = bitmap.GetSubBitmap( Rect(0, 0, bitmap.GetWidth(), bitmap.GetHeight())) dc = MemoryDC() bdc = BufferedDC(dc) bdc.SelectObject(bitmap) bdc.DrawBitmap(bitmap_to_draw, x, y) if draw_frame: # Black rect to support white pages bdc.SetPen(BLACK_PEN) bdc.SetBrush(TRANSPARENT_BRUSH) bdc.DrawRectangle(x, y, w, h) # Grey rect to support black pages bdc.SetPen(GREY_PEN) bdc.SetBrush(TRANSPARENT_BRUSH) bdc.DrawRectangle(x + 1, y + 1, w - 2, h - 2) bdc.SelectObject(NullBitmap) return bitmap
def scale_wx_bitmap(bitmap: wx.Bitmap, width, height, nearest=False): image: wx.Image = bitmap.ConvertToImage() image = image.Scale( width, height, wx.IMAGE_QUALITY_NEAREST if nearest else wx.IMAGE_QUALITY_HIGH) return image.ConvertToBitmap()
def wxbit_to_wx(bit_map: wx.Bitmap): ''' Converts a wx bitmap to a wx image ''' return bit_map.ConvertToImage()