双线性插值其实就是 根据需要resize的大小和原始图像的大小之比得到缩放比例,然后在求目标图的每一个像素的时候,根据这个比例找到其应该在原图的位置,这个位置可能会是小数,就用到该位置周围4个整数像素点的值来计算该位置的像素,然后离该位置距离近的点所占该位置值的权重要大一些.实现代码如下,效率有点低,待改进
from PIL import Image
from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
import math
import os
img_path = r'C:\Users\king\Pictures\简历\微信图片_20190731154219.jpg'
img_arr = np.array(Image.open(img_path), np.uint8)
target_size = (800, 600, 3)
cur_size = img_arr.shape
ratio_x, ratio_y = target_size[0] / cur_size[0], target_size[1] / cur_size[1]
def cal(x, y, c):
t_left_x, t_left_y = math.floor(x), math.floor(