tkinter を使用して、写真をズームする機能を簡単に実現する
最初にレンダリングを見てください。
動的レンダリングも見ることができます:
ディレクトリ構造は次のとおりです。
モジュールを使用します。
- 標準モジュール:
math
tkinter - サードパーティ モジュール:
PIL
コードは以下のように表示されます:
import math
import tkinter as tk
from PIL import Image,ImageTk
root = tk.Tk()
root.title('缩放图片')
root.geometry('700x500+300+100')
default_val = 50
scale_var=tk.IntVar()
scale_var.set(default_val)
scale = tk.Scale(root,orient=tk.HORIZONTAL,variable=scale_var)
scale.pack(fill="x")
img = Image.open("scale.png")
img1 = ImageTk.PhotoImage(img)
imgLabel = tk.Label(root,image=img1)
imgLabel.pack(fill="both")
w,h = img.size
scalew=math.ceil( w/default_val )
scaleh=math.ceil( h/default_val )
def scaleFn(val):
# print(val)
if int(val) <= 10:#限制
return
targetw = int(val)*scalew
targeth = int(val)*scaleh
bg = img.resize((targetw, targeth))
bgg = ImageTk.PhotoImage(bg)
imgLabel.config(image=bgg)
imgLabel.image = bgg
scale.config(command=lambda x:scaleFn(x))
root.mainloop()
----END----
学習のみ。