日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tkinter实现图片自适应

發布時間:2023/12/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tkinter实现图片自适应 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載自大神 https://blog.csdn.net/sinat_27382047 的博客
謝謝分享!!!!!

import io from PIL import Image, ImageTk import tkinter as tk def resize(w, h, w_box, h_box, pil_image): ''' resize a pil_image object so it will fit into a box of size w_box times h_box, but retain aspect ratio 對一個pil_image對象進行縮放,讓它在一個矩形框內,還能保持比例 ''' f1 = 1.0*w_box/w # 1.0 forces float division in Python2 f2 = 1.0*h_box/h factor = min([f1, f2]) #print(f1, f2, factor) # test # use best down-sizing filter width = int(w*factor) height = int(h*factor) return pil_image.resize((width, height), Image.ANTIALIAS) root = tk.Tk() # size of image display box you want #期望圖像顯示的大小 w_box = 800 h_box = 800 # open as a PIL image object #以一個PIL圖像對象打開 pil_image = Image.open(r'C:\Users\23216\Desktop\1.jpg') # get the size of the image #獲取圖像的原始大小 w, h = pil_image.size # resize the image so it retains its aspect ration # but fits into the specified display box #縮放圖像讓它保持比例,同時限制在一個矩形框范圍內 pil_image_resized = resize(w, h, w_box, h_box, pil_image) # convert PIL image object to Tkinter PhotoImage object # 把PIL圖像對象轉變為Tkinter的PhotoImage對象 tk_image = ImageTk.PhotoImage(pil_image_resized) # put the image on a widget the size of the specified display box # Label: 這個小工具,就是個顯示框,小窗口,把圖像大小顯示到指定的顯示框 label = tk.Label(root, image=tk_image, width=w_box, height=h_box) #padx,pady是圖像與窗口邊緣的距離 label.pack(padx=5, pady=5) root.mainloop()

總結

以上是生活随笔為你收集整理的tkinter实现图片自适应的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。