python展示图片的四种方法
    		       		warning:
    		            这篇文章距离上次修改已过438天,其中的内容可能已经有所变动。
    		        
        		                
                在Python中,可以使用多种库来显示图片,以下是四种常见的方法:
- 使用Pillow库
 
Pillow是Python中一个强大的图片处理库,可以用它来显示图片。
from PIL import Image
image = Image.open('image.jpg')
image.show()- 使用matplotlib库
 
matplotlib是Python中一个强大的数据可视化库,也可以用来显示图片。
import matplotlib.pyplot as plt
from matplotlib.image import imread
 
img = imread('image.jpg')
plt.imshow(img)
plt.show()- 使用OpenCV库
 
OpenCV是一个开源的计算机视觉库,它也可以用来显示图片。
import cv2
 
img = cv2.imread('image.jpg')
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()- 使用Tkinter库
 
Tkinter是Python内置的GUI库,可以用来创建简单的图形界面,也可以用来显示图片。
from tkinter import Tk, Image, Label
from PIL.ImageTk import PhotoImage
 
root = Tk()
 
image = PhotoImage(file='image.png')
label = Label(image=image)
label.pack()
 
root.mainloop()以上四种方法各有优缺点,可以根据实际需求选择合适的方法。
评论已关闭