You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python脚本中如何保存plt.imshow(spline16插值)后的图像?

How to Save Interpolated Images from plt.imshow()

Great question! The issue here is that plt.imsave() operates directly on your original 3x3 data array and doesn't apply the interpolation you specified in imshow(). To save the smoothed, interpolated version exactly as it appears when you render it, you need to capture the rendered image from the matplotlib figure canvas. Here are two straightforward methods to achieve this:

Method 1: Save the Figure Directly (Simplest Approach)

This method saves the entire figure (with your interpolated image) while removing any axes or extra padding to get a clean output.

import matplotlib.pyplot as plt
import numpy as np

# Example 3x3 data matrix
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Create a figure and axis
fig, ax = plt.subplots()

# Plot the image with your desired interpolation
ax.imshow(data, interpolation='spline16')

# Turn off axes to avoid unwanted borders
ax.axis('off')

# Optional: Set figure size and DPI for desired output resolution
# For a 600x600 pixel image: 2 inches * 300 DPI = 600 pixels
fig.set_size_inches(2, 2)

# Save the figure with no padding
plt.savefig('interpolated_output.png', 
            bbox_inches='tight', 
            pad_inches=0, 
            dpi=300)

# Clean up the plot
plt.close()

Key Notes:

  • Adjust fig.set_size_inches() and dpi to control the final pixel dimensions of your saved image.
  • bbox_inches='tight' and pad_inches=0 ensure no extra white space around the image.

Method 2: Extract Rendered Pixel Data and Save

If you want to get the exact pixel array of the interpolated image (for further processing or saving with plt.imsave()), you can extract it directly from the figure canvas.

import matplotlib.pyplot as plt
import numpy as np

data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

fig, ax = plt.subplots()
ax.imshow(data, interpolation='spline16')
ax.axis('off')

# Force the figure to render (critical for extracting pixel data)
fig.canvas.draw()

# Get the dimensions of the rendered canvas
width, height = fig.canvas.get_width_height()

# Extract RGB pixel data from the canvas
image_pixels = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
image_pixels = image_pixels.reshape(height, width, 3)

# Save the extracted interpolated image
plt.imsave('interpolated_pixels.png', image_pixels)

plt.close()

Key Notes:

  • fig.canvas.draw() ensures the interpolation is applied and the image is rendered before extracting pixels.
  • This method gives you a numpy array of the interpolated image, which you can manipulate further if needed.

Both methods will save the image exactly as it appears when you use plt.imshow(data, interpolation='spline16'), solving the problem of plt.imsave() not respecting the interpolation setting.

内容的提问来源于stack exchange,提问作者Sarques

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:20:20