首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >[Python][转载]比例缩放图片不变形

[Python][转载]比例缩放图片不变形

作者头像
云未归来
发布2025-07-18 16:04:10
发布2025-07-18 16:04:10
1090
举报

import cv2 import numpy as np

def resizeAndPad(img, size, padColor=0):     h, w = img.shape[:2]     sh, sw = size

    # interpolation method     if h > sh or w > sw:  # shrinking image         interp = cv2.INTER_AREA     else:  # stretching image         interp = cv2.INTER_CUBIC

    # aspect ratio of image     aspect = w / h  # if on Python 2, you might need to cast as a float: float(w)/h

    # compute scaling and pad sizing     if aspect > 1:  # horizontal image         new_w = sw         new_h = np.round(new_w / aspect).astype(int)         pad_vert = (sh - new_h) / 2         pad_top, pad_bot = np.floor(pad_vert).astype(int), np.ceil(pad_vert).astype(int)         pad_left, pad_right = 0, 0     elif aspect < 1:  # vertical image         new_h = sh         new_w = np.round(new_h * aspect).astype(int)         pad_horz = (sw - new_w) / 2         pad_left, pad_right = np.floor(pad_horz).astype(int), np.ceil(pad_horz).astype(int)         pad_top, pad_bot = 0, 0     else:  # square image         new_h, new_w = sh, sw         pad_left, pad_right, pad_top, pad_bot = 0, 0, 0, 0

    # set pad color     if len(img.shape) is 3 and not isinstance(padColor,                                               (list, tuple, np.ndarray)):  # color image but only one color provided         padColor = [padColor] * 3

    # scale and pad     scaled_img = cv2.resize(img, (new_w, new_h), interpolation=interp)     scaled_img = cv2.copyMakeBorder(scaled_img, pad_top, pad_bot, pad_left, pad_right, borderType=cv2.BORDER_CONSTANT,                                     value=padColor)

    return scaled_img

def resize_img_keep_ratio(img_name, target_size, color=(255, 255, 255)):     img = cv2.imread(img_name)     old_size = img.shape[0:2]     # ratio = min(float(target_size)/(old_size))     ratio = min(float(target_size[i]) / (old_size[i]) for i in range(len(old_size)))     new_size = tuple([int(i * ratio) for i in old_size])     img = cv2.resize(img, (new_size[1], new_size[0]))     pad_w = target_size[1] - new_size[1]     pad_h = target_size[0] - new_size[0]     top, bottom = pad_h // 2, pad_h - (pad_h // 2)     left, right = pad_w // 2, pad_w - (pad_w // 2)     img_new = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, None, color)     return img_new

if __name__ == '__main__':     # 第一种方法     v_img = cv2.imread('v.jpg')  # vertical image     scaled_v_img = resizeAndPad(v_img, (200, 200), 127)

    # 第二种方法(推荐)     img = 'D:/test1.jpg'     target_size = [500, 100]     new_image = resize_img_keep_ratio(img, target_size)     cv2.imshow('result', new_image)     cv2.waitKey(0)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-05-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档