{"id":1151,"date":"2024-01-09T05:22:21","date_gmt":"2024-01-09T05:22:21","guid":{"rendered":"https:\/\/snersbots.co.uk\/?p=1151"},"modified":"2024-01-09T05:22:21","modified_gmt":"2024-01-09T05:22:21","slug":"wxpython-background-image-scaling","status":"publish","type":"post","link":"https:\/\/snersbots.co.uk\/index.php\/wxpython-background-image-scaling\/","title":{"rendered":"wxPython Background Image Scaling"},"content":{"rendered":"\n<p>What is a user interface without a background image? Not only does it need an image, it also needs to automatically resize to fit, as the user resizes the user interface. The photo used here is my original photo. It is only for exercising the code. I don&#8217;t have any intention of using this exact image in the final version.<\/p>\n\n\n\n<p> <\/p>\n\n\n\n<!--more-->\n\n\n\n<p><\/p>\n\n\n\n<p>It&#8217;s interesting to note that most of the sample code I found is based around the same few examples. The example I used in the end came from <a href=\"https:\/\/stackoverflow.com\/questions\/21369877\/rescale-image-when-parent-is-resized-in-wxpython\">Stack Overflow<\/a>. This is a good starting point because it works.<\/p>\n\n\n\n<p>nicely with the window.<\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#e9f1f7\"><code>import wx\n\nclass MainPanel(wx.Panel):\n    def __init__(self, parent):\n        wx.Panel.__init__(self, parent, -1, style=wx.FULL_REPAINT_ON_RESIZE)\n        self.Bind(wx.EVT_PAINT, self.OnPaint)\n        self.img = wx.Image('daffodils.jpg', wx.BITMAP_TYPE_JPEG)\n        self.imgx, self.imgy = self.img.GetSize()\n\n    def OnPaint(self, event):\n        dc = wx.PaintDC(self)\n        dc.Clear()\n        x,y = self.GetSize()\n        posx,posy = 0, 0\n        newy = int(float(x)\/self.imgx*self.imgy)\n        if newy &lt; y:\n            posy = int((y - newy) \/ 2) \n            y = newy\n        else:\n            newx = int(float(y)\/self.imgy*self.imgx)\n            posx = int((x - newx) \/ 2)\n            x = newx        \n\n        img = self.img.Scale(x,y, wx.IMAGE_QUALITY_HIGH)\n        self.bmp = wx.Bitmap(img)\n        dc.DrawBitmap(self.bmp,posx,posy)\n\nclass MainFrame(wx.Frame):\n    def __init__(self, parent):\n        wx.Frame.__init__(self, parent, -1, title='Test', size=(600,400))\n        self.panel = MainPanel(self)\n        self.Show()\n\napp = wx.App(0)\nframe = MainFrame(None)\napp.MainLoop()    <\/code><\/pre>\n\n\n\n<p>In the OnPaint(): function, it does the calculation for keeping the image in scale. This particular algorithm keeps the aspect ratio correct. So this is what we will need when actually showing photos. But for the background picture I want it to forget the aspect ratio. <\/p>\n\n\n\n<p>From the last line in the MainPanel init, we see that imgx and imgy are the original height and width of the image.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        self.imgx, self.imgy = self.img.GetSize()<\/code><\/pre>\n\n\n\n<p>I am going to rename this to img_height and img_width. This is confusing.<\/p>\n\n\n\n<p>This is how it looks at the moment, if the UI is scaled:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"360\" height=\"571\" src=\"http:\/\/snersbots.co.uk\/wp-content\/uploads\/2024\/01\/daffodils_portrait.png\" alt=\"When the user interface goes tall and narrow, white space appears above and below the image.\" class=\"wp-image-1160\" srcset=\"https:\/\/snersbots.co.uk\/wp-content\/uploads\/2024\/01\/daffodils_portrait.png 360w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2024\/01\/daffodils_portrait-189x300.png 189w\" sizes=\"auto, (max-width: 360px) 100vw, 360px\" \/><figcaption class=\"wp-element-caption\">When the user interface goes tall and narrow, white space appears above and below the image.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"976\" height=\"372\" src=\"http:\/\/snersbots.co.uk\/wp-content\/uploads\/2024\/01\/daffodils_landscape.png\" alt=\"When the user interface goes wide and narrow, white space appears to the left and right of the image.\" class=\"wp-image-1162\" srcset=\"https:\/\/snersbots.co.uk\/wp-content\/uploads\/2024\/01\/daffodils_landscape.png 976w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2024\/01\/daffodils_landscape-300x114.png 300w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2024\/01\/daffodils_landscape-768x293.png 768w\" sizes=\"auto, (max-width: 976px) 100vw, 976px\" \/><figcaption class=\"wp-element-caption\">When the user interface goes wide and narrow, white space appears to the left and right of the image.<\/figcaption><\/figure>\n\n\n\n<p>To make the image scale with the user interface, as you would expect for a background picture, you can simply remove the lines which recalculate x and y &#8211; or as it will be called &#8211; width and height. Here is the code that scales ignoring the aspect ratio.<\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#e9f1f7\"><code>import wx\n\nclass MainPanel(wx.Panel):\n    def __init__(self, parent):\n        wx.Panel.__init__(self, parent, -1, style=wx.FULL_REPAINT_ON_RESIZE)\n        self.Bind(wx.EVT_PAINT, self.OnPaint)\n        self.img = wx.Image('daffodils.jpg', wx.BITMAP_TYPE_JPEG)\n        self.imgx, self.imgy = self.img.GetSize()\n\n    def OnPaint(self, event):\n        dc = wx.PaintDC(self)\n        dc.Clear()\n        x,y = self.GetSize()\n        posx,posy = 0, 0\n  \n        # Deleted lines here\n        \n        img = self.img.Scale(x,y, wx.IMAGE_QUALITY_HIGH)\n        self.bmp = wx.Bitmap(img)\n        dc.DrawBitmap(self.bmp,posx,posy)\n\nclass MainFrame(wx.Frame):\n    def __init__(self, parent):\n        wx.Frame.__init__(self, parent, -1, title='Test', size=(600,400))\n        self.panel = MainPanel(self)\n        self.Show()\n\napp = wx.App(0)\nframe = MainFrame(None)\napp.MainLoop()    <\/code><\/pre>\n\n\n\n<p>This is good now. Next task is to get a quieter background image, that won&#8217;t take the focus.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is a user interface without a background image? Not only does it need an image, it also needs to automatically resize to fit, as the user resizes the user interface. The photo used here is my original photo. It is only for exercising the code. I don&#8217;t have any intention of using this exact <a class=\"read-more\" href=\"https:\/\/snersbots.co.uk\/index.php\/wxpython-background-image-scaling\/\">The full story here&#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":1154,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,8],"tags":[69,71],"class_list":["post-1151","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-desktop-apps","category-software","tag-duplicate-photos","tag-python"],"_links":{"self":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/1151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=1151"}],"version-history":[{"count":12,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/1151\/revisions"}],"predecessor-version":[{"id":1167,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/1151\/revisions\/1167"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media\/1154"}],"wp:attachment":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=1151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/categories?post=1151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/tags?post=1151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}