写一个python程序,能展示写一个python程序,能展示图片图片
时间: 2024-12-16 20:19:26 浏览: 8
在Python中,我们可以使用PIL(Python Imaging Library)或者更现代的库如Pillow来处理和显示图片。这里是一个简单的例子,它展示了如何从本地文件加载图片并显示出来:
```python
from PIL import Image
def display_image(image_path):
try:
# 打开图片
img = Image.open(image_path)
# 显示图片
img.show()
print(f"Image '{image_path}' displayed successfully.")
except FileNotFoundError:
print(f"File '{image_path}' not found.")
except Exception as e:
print(f"An error occurred while displaying the image: {e}")
# 要显示的图片路径(假设是.jpg格式)
image_path = "path_to_your_image.jpg"
display_image(image_path)
阅读全文