在Python中,你可以使用多个库来导入图片,以下是使用Pillow和Matplotlib两个库导入图片的示例:
使用Pillow库导入图片
1. 安装Pillow库:
pip install Pillow
2. 导入图片的代码示例:
from PIL import Image
打开图片文件
image = Image.open('image.jpg')
显示图片
image.show()
Pillow库支持多种图片格式,包括JPEG、PNG、GIF、BMP等。
使用Matplotlib库导入图片
1. 安装Matplotlib库:
pip install matplotlib
2. 导入图片的代码示例:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
读取图片文件
image = mpimg.imread('image.jpg')
显示图片
plt.imshow(image)
plt.show()
同样,Matplotlib库也支持多种图片格式。