tensorflow2实现不失真比例的resize

tensorflow2实现不失真比例的resize_第1张图片

假设我现在有一张(h, w)= (392, 452) 的图片需要不失真比例的resize成(400, 300),可以使用tensorflow2的api:

import warnings
warnings.filterwarnings("ignore")
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

import tensorflow as tf
import matplotlib.pyplot as plt
import cv2

img = cv2.imread('img_1.png')
print('img.shape =',img.shape)

img_new = tf.image.resize_with_pad(img,400,300,'nearest',True)
print('img_new.shape =',img_new.shape)

figure = plt.figure(figsize=(6,4),dpi=200)
plt.subplot(1,2,1)
plt.imshow(img[::-1])
plt.subplot(1,2,2)
plt.imshow(img_new[::-1])
plt.show()

tensorflow2实现不失真比例的resize_第2张图片

 

img.shape = (392, 452, 3)
img_new.shape = (400, 300, 3)

你可能感兴趣的:(tensorflow2,深度学习,tensorflow,人工智能,python)