python常用图像缩放函数

 图像缩放到固定大小

pillow库

out = im.resize((128, 128))

opencv库

res=cv2.resize(image,(32,32),interpolation=cv2.INTER_CUBIC)

skimage库

dst=transform.resize(img, (80, 60))

封装好的缩放函数(自取)

import os
import sys
from pathlib import Path
import numpy as np
from PIL import Image
import cv2


def scale_image_pil(img_pil, width=None, height=None):
    """
    如果指定宽,就等比例缩放到指定宽;
    如果指定高,就等比例缩放到指定高;
    如果指定高宽,就直接缩放到指定高宽;
    :param img_pil:图片
    :param width:目标宽度
    :param height:目标高度
    :return:缩放后的图像
    """
    width_, height_ = img_pil.size

    if width is None and height is None:
        width_dst = width_
        height_dst = height_
    elif width is not None and height is None:  # 如果指定了宽,就返回图像等比例缩放到指定宽时对应的高
        width_dst = width
        height_dst = int(width * height_ / width_)
    elif width is None and height is not None:  # 如果指定了高,就返回图像等比例缩放到指定高时对应的宽
        width_dst = int(height * width_ / height_ if height else width)
        height_dst = height
    else:
        width_dst = width
        height_dst = height

    img_pil_new = img_pil.resize((width_dst, height_dst))
    return img_pil_new


def scale_image_cv(img_cv, width=None, height=None):
    """
    如果指定宽,就等比例缩放到指定宽;
    如果指定高,就等比例缩放到指定高;
    如果指定高宽,就直接缩放到指定高宽;
    :param img_cv:图片
    :param width:目标宽度
    :param height:目标高度
    :return:缩放后的图像
    """
    height_, width_ = img_cv.shape[:2]

    if width is None and height is None:
        width_dst = width_
        height_dst = height_
    elif width is not None and height is None:  # 如果指定了宽,就返回图像等比例缩放到指定宽时对应的高
        width_dst = width
        height_dst = int(width * height_ / width_)
    elif width is None and height is not None:  # 如果指定了高,就返回图像等比例缩放到指定高时对应的宽
        width_dst = int(height * width_ / height_ if height else width)
        height_dst = height
    else:
        width_dst = width
        height_dst = height

    im_cv_new = cv2.resize(img_cv, (int(width_dst), int(height_dst)))
    return im_cv_new


def resize_with_ratio_to_bounding_pil(img_pil, size=(512, 512)):
    """
    将原图bounding式缩放到指定尺寸;
    即给定bounding box大小,将图像等比例缩放后嵌入到bounding box中
    :param img_pil:
    :param size: size为tuple,可以长宽不同
    :return:
    """
    [h, w] = size
    ww, hh = img_pil.size
    scale = min(h / hh, w / ww)  # 指定高宽相同时,是按原图大的边进行缩放
    img_pil_new = img_pil.resize((int(ww * scale), int(hh * scale)))
    return img_pil_new


def resize_with_ratio_to_bounding_cv(img_cv, size=(512, 512)):
    """
    将原图bounding式缩放到指定尺寸;
    即给定bounding box大小,将图像等比例缩放后嵌入到bounding box中
    :param img_cv:
    :param size: size为tuple,可以长宽不同
    :return:
    """
    [h, w] = size
    hh, ww = img_cv.shape[0], img_cv.shape[1]
    scale = min(h / hh, w / ww)  # 指定高宽相同时,是按原图大的边进行缩放
    im_cv_new = cv2.resize(img_cv, (int(ww * scale), int(hh * scale)))
    return im_cv_new


if __name__ == '__main__':
    #######################################################################
    # img_ppath = Path(r"fengjing.jpg")
    # img_pil = Image.open(img_ppath.as_posix())
    # print(img_pil.size[::-1])
    #
    # print(scale_image_pil(img_pil).size[::-1])
    # print(scale_image_pil(img_pil, width=123, height=456).size[::-1])
    # print(scale_image_pil(img_pil, width=200).size[::-1])
    # print(scale_image_pil(img_pil, height=45).size[::-1])
    #
    # """
    # (1200, 1920)
    # (1200, 1920)
    # (456, 123)
    # (125, 200)
    # (45, 72)
    # """

    #################################################################
    img_ppath = Path(r"fengjing.jpg")
    img_c3_cv = cv2.imread(img_ppath.as_posix(), cv2.IMREAD_COLOR)
    print(img_c3_cv.shape)

    print(scale_image_cv(img_c3_cv).shape)
    print(scale_image_cv(img_c3_cv, width=123, height=456).shape)
    print(scale_image_cv(img_c3_cv, width=200).shape)
    print(scale_image_cv(img_c3_cv, height=45).shape)

    """
    (1200, 1920, 3)
    (1200, 1920, 3)
    (456, 123, 3)
    (125, 200, 3)
    (45, 72, 3)
    """

    #######################################################################
    # img_ppath = Path(r"fengjing.jpg")
    # img_c3_cv = cv2.imread(img_ppath.as_posix(), cv2.IMREAD_COLOR)
    # print(img_c3_cv.shape)
    #
    # print(resize_with_ratio_to_bounding_cv(img_c3_cv, (512, 512)).shape)
    # print(resize_with_ratio_to_bounding_cv(img_c3_cv, (255, 512)).shape)
    # print(resize_with_ratio_to_bounding_cv(img_c3_cv, (512, 255)).shape)
    # print(resize_with_ratio_to_bounding_cv(img_c3_cv).shape)
    #
    # """
    # (1200, 1920, 3)
    # (320, 512, 3)
    # (255, 408, 3)
    # (159, 255, 3)
    # (320, 512, 3)
    # """

    ##################################################################
    # img_ppath = Path(r"Dfengjing.jpg")
    # img_c3_pil = Image.open(img_ppath.as_posix()).convert("RGB")
    # print(img_c3_pil.size[::-1])
    # print(resize_with_ratio_to_bounding_pil(img_c3_pil, (512, 512)).size[::-1])
    # print(resize_with_ratio_to_bounding_pil(img_c3_pil, (255, 512)).size[::-1])
    # print(resize_with_ratio_to_bounding_pil(img_c3_pil, (512, 255)).size[::-1])
    # print(resize_with_ratio_to_bounding_pil(img_c3_pil).size[::-1])
    #
    # """
    # (1200, 1920)
    # (320, 512)
    # (255, 408)
    # (159, 255)
    # (320, 512)
    # """

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wugou2014

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值