iT邦幫忙

0

用 tkinter 實現選擇路徑打開照片並顯示照片(有解決中文路徑、中文檔名問題)

  • 分享至 

  • xImage
  •  
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from tkinter import filedialog
import tkinter as tk
from PIL import ImageTk, Image
root = tk.Tk()
videoFrame = tk.Frame(root).pack()
video = tk.Label(videoFrame)
video.pack()
def cv_imread(filePath):
    cv_img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1)
    return cv_img
def oas():
    sfname = filedialog.askopenfilename(title='選擇',
                                        filetypes=[
                                            ('All Files','*'),
                                            ("jpeg files","*.jpg"),
                                            ("png files","*.png"),
                                            ("gif files","*.gif")])

    im = cv_imread(sfname)
    cv2image = cv2.cvtColor(im, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    video.imgtk = imgtk
    video.configure(image=imgtk)

B1 = tk.Button(root, text="打開",command = oas).pack()
root.mainloop()

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言