行业知识
python展示图片的四种方法
2026-11-28 20:31:17
来源:互联网转载
在 Python ��,有多种库可用于图像展示。以下是一些常见的展示图片的方法和相关的库
()-
Matplotlib:
- 使用 matplotlib.pyplot.imshow 函数展示图像。
- 示例代码:
import matplotlib.pyplot as pltimport matplotlib.image as mpimgimg_path = 'example.jpg'img = mpimg.imread(img_path)plt.imshow(img)plt.axis('off')plt.show()
Pillow(PIL):
()- 使用 PIL.Image.show 方法展示图像。
- 示例代码:
from PIL import Imageimg_path = 'example.jpg'img = Image.open(img_path)img.show()
IPython 的 Display 模块:
- 使用 IPython.display.Image 类展示图像。
- 示例代码:
from IPython.display import Image, displayimg_path = 'example.jpg'display(Image(filename=img_path))
OpenCV:
- 使用 cv2.imshow 方法展示图像。需要注意的是,cv2.imshow 通常在 Jupyter Notebook 中无法直接使用,但在独立的 Python 脚本中有效。
- 示例代码:
import cv2img_path = 'example.jpg'img = cv2.imread(img_path)cv2.imshow('Image', img)cv2.waitKey(0)cv2.destroyAllWindows()
上一篇:前端如何认证服务器?
下一篇:高效注册大量微信号的秘籍与技巧