Hi resizing an wx.image and keep proportions question

Hi, can anyone help with keeping the x or y proportions of an image when scaling e.g

pic = wx.Image(self.filename, wx.BITMAP_TYPE_ANY).Scale(285, 280)
self.pickedImage = wx.BitmapFromImage(pic, -1)
self.staticBitmap1.SetBitmap(self.pickedImage )
self.staticBitmap1.Show
(True)

Here im scaling to 285 x 280 but is it possible to scale to 285 x (whatever keeps the height proportions of the intial image when scaled down to 285px wide)

Thanks for any help with this (especially examples :))

regards

Dan

I couldn’t find a “keep proportions” in PIL or wx, so did the below. If you want to scale the image to 285 x 280, do “_Scale(wximage, 285, 0)”

The code is probably a bit sucky, I’m sure it shoulda been doable in a better way. But I don’t do math! :slight_smile: And it basically worked.

def _Scale(im, maxWidth, maxHeight):
oldWidth, oldHeight = im.GetWidth(), im.GetHeight()
newHeight = 0
if oldWidth > maxWidth:
newWidth = maxWidth
newHeight = oldHeight * (float(newWidth) / oldWidth)

else:
    newWidth = oldWidth
   
if oldHeight > maxHeight:
    newHeight = maxHeight
    newWidth = oldWidth * (float(newHeight) / oldHeight)

newHeight = int(newHeight)

newWidth = int(newWidth)

if not newWidth:
    newWidth = oldWidth
if not newHeight:
    newHeight = oldHeight 
   
return newWidth, newHeight
···

On 2/26/07, aus stuff austuff@gmail.com wrote:

Hi, can anyone help with keeping the x or y proportions of an image when scaling e.g

pic = wx.Image(self.filename, wx.BITMAP_TYPE_ANY).Scale(285, 280)
self.pickedImage = wx.BitmapFromImage(pic, -1)
self.staticBitmap1.SetBitmap(self.pickedImage )

    self.staticBitmap1.Show

(True)

Here im scaling to 285 x 280 but is it possible to scale to 285 x (whatever keeps the height proportions of the intial image when scaled down to 285px wide)

Thanks for any help with this (especially examples :))

regards

Dan

aus stuff wrote:

Hi, can anyone help with keeping the x or y proportions of an image when scaling e.g
         pic = wx.Image(self.filename, wx.BITMAP_TYPE_ANY).Scale(285, 280)
        self.pickedImage = wx.BitmapFromImage(pic, -1)
        self.staticBitmap1.SetBitmap(self.pickedImage )
        self.staticBitmap1.Show (True)

A proportional resize is a very simple math problem, you just want to adjust the alternate dimension by the same factor that you are adjusting the known dimension. In other words, given this algebraic equation:

  newWidth/oldWidth = newHeight/oldHeight

plug in the values that you know and solve for the new dimension that you don't know. Like this:

  400/800 = newHeight/600
  600 * 400/800 = newHeight
  newHeight = 300

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!