1

As mentioned in the title, how do I resize the pictures dynamically? Functionality: When I click "Other" button, an open menu is displayed. The optionmenu displays images from a folder that I have specified in the code. All the images are in different sizes. How can I maintain the aspect ratio while resizing them?

...
def otherdrops():
    other_opt = wother
    other_opt.config(width=50, font=('Helvetica', 12))
    other_opt.pack()   
def other_images(wother):
    print(wother)  # selected option
    other_label.config(image=other[wother])
other_label = tk.Label(otherframe)
other_label.pack(side = 'bottom', pady=padylength)
other = {}
for other_name in tradinglists.tradingotherimages:
    other[other_name] = ImageTk.PhotoImage(Image.open("/Images/{}.png".format(other_name)))
othervariable = tk.StringVar(tab2)
othervariable.set(tradinglists.tradingotherimages[0])
wother = tk.OptionMenu(otherframe, othervariable, *tradinglists.tradingotherimages, command=other_images)
def refreshother():
    otherframe.pack_forget() if otherframe.winfo_manager() else otherframe.pack(anchor='center')
other_k = tk.Button(wavebtnframe, bg = "red", text="Other", width = artbtn_width, height = btnsize_height, command=lambda:[otherdrops(), refreshother()])
other_k.pack(side = 'left', padx=wavebtnspadx, pady=padylength)

V2:

def importImageWithResize(filename):
    img = Image.open(filename)
    width, height = img.size
    ratio = width / height
    new_height = 20
    new_width = new_height * ratio
    return img.resize((width, height))

def otherdrops():
    other_opt = wother
    other_opt.config(width=50, font=('Helvetica', 12))
    other_opt.pack()   
def other_images(wother):
    print(wother)  # selected option
    other_label.config(image=other[wother])
other_label = tk.Label(otherframe)
other_label.pack(side = 'bottom', pady=padylength)
other = {}
for other_name in tradinglists.tradingotherimages:
    other[other_name] = ImageTk.PhotoImage(importImageWithResize("./Images/{}.png".format(other_name)))
othervariable = tk.StringVar(tab2)
othervariable.set(tradinglists.tradingotherimages[0])
wother = tk.OptionMenu(otherframe, othervariable, *tradinglists.tradingotherimages, command=other_images)
def refreshother():
    otherframe.pack_forget() if otherframe.winfo_manager() else otherframe.pack(anchor='center')
other_k = tk.Button(wavebtnframe, bg = "red", text="Other", width = artbtn_width, height = btnsize_height, command=lambda:[otherdrops(), refreshother()])
other_k.pack(side = 'left', padx=wavebtnspadx, pady=padylength)
Advik
  • 43
  • 9
  • `ratio = old_height/old_width` and later `new_width = some_value` and `new_height = new_width * ratio`. And use module `PIL`/`pillow` to resize it. – furas Sep 07 '20 at 04:41
  • 1
    See [how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio](https://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio). – acw1668 Sep 07 '20 at 04:57

1 Answers1

1

You can resize images with image_name.resize((width, height)) I'd make a method like this:

def importImageWithResize(filename):
    img = Image.open(filename)
    width, height = img.size
    ratio = width / height
    new_height = preset_height
    new_width = int(new_height * ratio)
    return img.resize((new_width, new_height ))

And then change your import line to match:

other[other_name] = ImageTk.PhotoImage(importImageWithResize("/Images/{}.png".format(other_name)))

I assumed you had a set height you want them all to match, but you could change that to have a preset width or whatever you want. If you have specific rules for the size you want and need help, feel free to ask for examples.

Let us know if you have more questions etc.

Joel Toutloff
  • 464
  • 3
  • 4
  • 1
    Hey, thanks for the help! I tried what you suggested and got an error. ValueError: Unknown resampling filter (350). Use Image.NEAREST (0), Image.LANCZOS (1), Image.BILINEAR (2), Image.BICUBIC (3), Image.BOX (4) or Image.HAMMING (5) I entered preset height as 20. – Advik Sep 08 '20 at 18:02
  • I mistyped the resize line, sorry; I will edit the answer. img.resize(width, height) is supposed to be img.resize((width, height)) with double parenthesis – Joel Toutloff Sep 08 '20 at 18:42
  • 1
    So its not giving me any errors now and I'm able to run the application but I don't see any change in the height or the size in general. I updated my original post with version 2. Can you please check if I'm doing anything wrong? – Advik Sep 08 '20 at 21:57
  • 2
    `img.resize((width, height))` should be `img.resize((new_width, new_height))`. – acw1668 Sep 09 '20 at 05:39
  • 1
    Thanks guys! ```new_height * ratio``` was giving a float result so changed it to ```int(new_height * ratio)```. Works perfectly now! – Advik Sep 09 '20 at 16:12
  • oh yup; I messed that up to...geez; I should really run these things before I post them; I'll fix that too in case someone else comes around with the same issue – Joel Toutloff Sep 09 '20 at 16:52