将ax[1].set_title('Compressed Image')显示的图像保存为图像文件
时间: 2024-02-16 13:05:34 浏览: 68
好的,以下是将显示的压缩后图像保存为图像文件的代码:
```python
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
# 加载原始图像和压缩后的图像
original_image = Image.open('0.jpg')
compressed_image = Image.open('1.jpg')
# 将图像转换为numpy数组
original_array = np.array(original_image)
compressed_array = np.array(compressed_image)
# 计算image_norm
image_norm = np.linalg.norm(original_array)
# 计算MSE
diff = original_array - compressed_array
mse = np.mean(np.square(diff))
# 创建画布和子图
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
# 显示原始图像
ax[0].imshow(original_image)
ax[0].set_title('Original Image')
# 显示压缩后的图像
ax[1].imshow(compressed_image)
ax[1].set_title('Compressed Image')
# 保存图像文件
compressed_image.save('compressed.png')
# 显示图像
plt.show()
```
在这个代码中,我们使用了`compressed_image.save('compressed.png')`将压缩后的图像保存为PNG文件,并将文件名设置为`compressed.png`。你可以将文件名更改为你想要的任何名称。运行代码后,你将在当前工作目录下找到保存的图像文件。
阅读全文
相关推荐


















