图像缩放到固定大小
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)
# """