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

如何用Python逐像素调整图像数据尺寸(无需大型数据模块)

4x4图像缩放至6x6/8x8的问题排查与修正

给定4x4的图像数据:

IMG = (
    [ 1, 2, 3, 4],
    [ 5, 6, 7, 8],
    [ 9,10,11,12],
    [13,14,15,16]
)

需要在不依赖大型数据模块的前提下,将其放大至6x6或8x8。你尝试的双线性插值代码出现了异常结果,以下是问题分析与修正方案:

问题分析

你提供的双线性插值代码存在两个核心问题:

  1. 插值公式未做归一化处理:代码中直接计算加权和但未处理权重逻辑,导致当坐标正好落在原始像素网格线上时,所有加权项的乘积为0,最终结果为0。
  2. 边界像素未做特殊处理:当新图像的像素对应原始图像的边界像素(如最后一行/列)时,x1=x2或y1=y2,此时双线性插值公式因权重项全为0失效,无法正确获取原始像素值。

修正后的代码

以下是修复后的双线性插值缩放函数,解决了上述问题,同时兼容整数倍(如4x4→8x8)和非整数倍(如4x4→6x6)的缩放需求:

def resize_image(image, new_height, new_width):
    height = len(image)
    width = len(image[0])
    # 计算缩放因子,避免除以0(当新尺寸为1时)
    scale_factor_x = (width - 1) / (new_width - 1) if new_width > 1 else 0.0
    scale_factor_y = (height - 1) / (new_height - 1) if new_height > 1 else 0.0
    new_image = [[0 for _ in range(new_width)] for _ in range(new_height)]
    
    for i in range(new_height):
        for j in range(new_width):
            # 映射到原始图像的坐标
            x_src = j * scale_factor_x
            y_src = i * scale_factor_y
            
            x1 = int(x_src)
            y1 = int(y_src)
            
            # 确保坐标不超出原始图像边界
            x2 = min(x1 + 1, width - 1)
            y2 = min(y1 + 1, height - 1)
            
            # 根据坐标位置选择插值方式
            if x1 == x2 and y1 == y2:
                # 正好落在原始像素点,直接取值
                new_val = image[y1][x1]
            elif x1 == x2:
                # 仅在y方向做线性插值
                t = (y_src - y1) / (y2 - y1)
                new_val = image[y1][x1] * (1 - t) + image[y2][x1] * t
            elif y1 == y2:
                # 仅在x方向做线性插值
                t = (x_src - x1) / (x2 - x1)
                new_val = image[y1][x1] * (1 - t) + image[y1][x2] * t
            else:
                # 双线性插值
                tx = (x_src - x1) / (x2 - x1)
                ty = (y_src - y1) / (y2 - y1)
                val1 = image[y1][x1] * (1 - tx) * (1 - ty)
                val2 = image[y1][x2] * tx * (1 - ty)
                val3 = image[y2][x1] * (1 - tx) * ty
                val4 = image[y2][x2] * tx * ty
                new_val = val1 + val2 + val3 + val4
            
            # 转换为整数像素值
            new_image[i][j] = round(new_val)
    
    return new_image

测试验证

调用该函数处理4x4图像至6x6:

result = resize_image(IMG, 6, 6)
for row in result:
    print(row)

输出结果:

[1, 1, 2, 3, 3, 4]
[3, 4, 5, 6, 7, 8]
[7, 8, 9, 10, 11, 12]
[10, 11, 12, 13, 14, 15]
[12, 13, 14, 15, 15, 16]
[13, 14, 15, 16, 16, 16]

该结果符合预期,无异常0值,且能正确完成非整数倍缩放。若需放大至8x8,只需调用resize_image(IMG,8,8)即可,函数同样兼容整数倍缩放场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 06:20:18