温馨提示×

python运行结果图片如何调出来

小亿
103
2024-05-28 18:58:09
栏目: 编程语言

要在Python程序中调用并显示图片,可以使用Python的PIL库(Python Imaging Library)或者OpenCV库。以下是如何使用这两种库显示图片的示例代码:

使用PIL库:

from PIL import Image

# 打开图片文件
img = Image.open('example.jpg')

# 显示图片
img.show()

使用OpenCV库:

import cv2

# 读取图片文件
img = cv2.imread('example.jpg')

# 显示图片
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

请确保在运行这些代码之前,安装了PIL库或OpenCV库。你可以通过pip来安装这些库:

pip install pillow
pip install opencv-python

然后将要显示的图片文件放在和Python程序相同的目录下,运行代码即可显示图片。

0