2. 在控制台下使用 python exp1.py显示2张tifi、jpeg格式图像记录过程。
时间: 2024-10-10 11:13:32 浏览: 60
在Python中,如果你想要在控制台环境下展示TIFF和JPEG格式的图片,并显示它们加载和处理的过程,你可以使用matplotlib库,特别是`imshow()`函数。首先,你需要确保已经安装了必要的库,如matplotlib和PIL(Python Imaging Library,用于处理图像)。
下面是一个简单的例子,假设你有两个文件`exp1_tiff.tif`和`exp1_jpeg.jpg`:
```python
import matplotlib.pyplot as plt
from PIL import Image
# 加载图像
def load_image(file_name):
try:
# TIFF
if file_name.endswith('.tif'):
image = Image.open(file_name)
# JPEG
elif file_name.endswith('.jpg'):
image = Image.open(file_name).convert('RGB') # 将JPEG转换为RGB模式以便显示
else:
print(f"Unsupported format: {file_name}")
return None
return image
except Exception as e:
print(f"Error loading image: {e}")
return None
# 显示图像
def display_image(image):
if image is not None:
plt.imshow(image)
plt.axis('off')
plt.show()
print(f"{file_name} loaded successfully.")
# 读取并显示图像
image_tiff = load_image("exp1_tiff.tif")
display_image(image_tiff)
image_jpeg = load_image("exp1_jpeg.jpg")
display_image(image_jpeg)
阅读全文
相关推荐












