Помогите с масштабированием фотографии в tkinter
Я делаю прогу для просмотра фото.
Проблема заключается в том, что фото открывается в первоначальном размере (больше окна программы) и если в него (окно) фото не помещается, то просто обрезается.
Мне нужно чтобы фото открывалось уменьшенным (скажем 700х500), как это сделать?
Вот код:
from tkinter import *
import PIL
from PIL import ImageTk, Image
#window options
window = Tk()
window.title('picture viewer(v0.04alpha)')
window.geometry('800x600')
window['bg'] = 'grey'
for i in range(3):
window.columnconfigure(i, weight=1, minsize=70)
window.rowconfigure(i, weight=1, minsize=70)
#img
pil_image = Image.open("C:/111.jpg")
image = ImageTk.PhotoImage(pil_image)
image_sprite = Label(window, image=image)
image_sprite.grid(row=1, column=1, padx=8)
label_pic_name = Label(window, text='<< lena.jpg >>')
label_pic_name.grid(row=2, column=1)
#bottons_top
frame_top = Frame(window, width=100, height=100)
frame_top.grid(row=0, column=1)
btn1 = Button(frame_top, text="1111",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
padx=5
)
btn1.grid(row=0, column=1)
btn2 = Button(frame_top, text="2222",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
)
btn2.grid(row=0, column=2)
btn3 = Button(frame_top, text="3333",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
padx=5
)
btn3.grid(row=0, column=3)
btn4 = Button(frame_top, text="4444",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
padx=5
)
btn4.grid(row=0, column=4)
#bottons_side
frame_side = Frame(window)
frame_side.grid(row=1, column=0, padx=10)
btn11 = Button(frame_side, text="1111",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
)
btn11.grid(row=1, column=0)
btn22 = Button(frame_side, text="2222",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
)
btn22.grid(row=2, column=0)
btn33 = Button(frame_side, text="3333",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
)
btn33.grid(row=3, column=0)
btn44 = Button(frame_side, text="4444",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
)
btn44.grid(row=4, column=0)
window.mainloop()
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Попробуйте так:
from tkinter import *
import PIL
from PIL import ImageTk, Image
#window options
window = Tk()
window.title('picture viewer(v0.04alpha)')
window.geometry('800x600')
window['bg'] = 'grey'
for i in range(3):
window.columnconfigure(i, weight=1, minsize=70)
window.rowconfigure(i, weight=1, minsize=70)
#img
pil_image = Image.open("image_test2.jpg")
pil_image = pil_image.resize((700, 500), Image.ANTIALIAS) # <----
image = ImageTk.PhotoImage(pil_image)
image_sprite = Label(window, image=image)
image_sprite.grid(row=1, column=1, padx=8)
label_pic_name = Label(window, text='<< image_test2.jpg >>')
label_pic_name.grid(row=2, column=1)
#bottons_top
frame_top = Frame(window, width=100, height=100)
frame_top.grid(row=0, column=1)
btn1 = Button(frame_top, text="1111",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
padx=5
)
btn1.grid(row=0, column=1)
btn2 = Button(frame_top, text="2222",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
)
btn2.grid(row=0, column=2)
btn3 = Button(frame_top, text="3333",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
padx=5
)
btn3.grid(row=0, column=3)
btn4 = Button(frame_top, text="4444",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
padx=5
)
btn4.grid(row=0, column=4)
#bottons_side
frame_side = Frame(window)
frame_side.grid(row=1, column=0, padx=10)
btn11 = Button(frame_side, text="1111",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
)
btn11.grid(row=1, column=0)
btn22 = Button(frame_side, text="2222",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
)
btn22.grid(row=2, column=0)
btn33 = Button(frame_side, text="3333",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
)
btn33.grid(row=3, column=0)
btn44 = Button(frame_side, text="4444",
background="#555",
foreground="#ccc",
activebackground='#557',
font="16",
)
btn44.grid(row=4, column=0)
window.mainloop()
image_test2.jpg



