在Python中,您可以使用不同的库来在窗口中显示图片,以下是使用`tkinter`和`matplotlib`两个库的示例:
使用`tkinter`显示图片
import tkinter as tkfrom PIL import Image, ImageTkdef choosepic():path = tkinter.filedialog.askopenfilename()img_open = Image.open(path)img = ImageTk.PhotoImage(img_open)labelShowImage.config(image=img)labelShowImage.image = imgapp = tk.Tk()app.title("显示图片")labelShowImage = tk.Label(app)labelShowImage.pack()button = tk.Button(app, text="选择图片", command=choosepic)button.pack()app.mainloop()

使用`matplotlib`显示图片
import matplotlib.pyplot as pltfrom matplotlib.image import imreadimg = imread('image.jpg') 替换为你的图片路径plt.imshow(img)plt.axis('off') 不显示坐标轴plt.show()
使用OpenCV显示图片
import cv2img = cv2.imread('image.jpg') 替换为你的图片路径cv2.imshow('image', img)cv2.waitKey(0)cv2.destroyAllWindows()
请根据您的需求选择合适的库,并替换示例代码中的图片路径为您要显示的图片文件路径。
