法一:
使用的是skimage的库,搞清楚输入输出就行 ,size是图片比例,
两倍就是size=2。
还见过其他的库也有resize功能,有空再补充。
输出时候要格式转换。
from skimage import transform as sktransform
from skimage import io
from PIL import Image
import numpy as np
def resize_face(src_path , ratio):
foreground_face = io.imread(src_path)
aug_size_y = int(foreground_face.shape[0] * ratio)
aug_size_x = int(foreground_face.shape[1] * ratio)
foreground_face = sktransform.resize(foreground_face, (aug_size_y, aug_size_x), preserve_range=True).astype(np.uint8)
im = Image.fromarray(foreground_face)
im.save(src_path.split('.')[0]+'X'+str(ratio)+'.png', quality=100)
path ='192110.jpg'
resize_face(path,ratio=0.4)
法二:
只使用PIL库就可以,这里的width和weight可以设定固定的,也可以按比例放大缩小。
‘’’
Image.NEAREST :低质量
Image.BILINEAR:双线性
Image.BICUBIC :三次样条插值
Image.ANTIALIAS:高质量
‘’’
from PIL import Image
def produceImage(file_in,file_out,ratio):
image = Image.open(file_in)
image_size = image.size
width ,height = int(image_size[0]*ratio),int(image_size[1]*ratio)
resized_image = image.resize((width, height), Image.ANTIALIAS)
resized_image.save(file_out)
input_path = 'test/4.png'
output_path = 'test/5.png'
produceImage(input_path,output_path,ratio=0.5)
法三:
使用opencv2
大同小异
import cv2
def fun2(input_path, output_path,ratio):
image = cv2.imread(input_path)
down_width = image.shape[0]
down_height = image.shape[1]
resized_down = cv2.resize(image, (int(down_width*ratio), int(down_height*ratio)), interpolation= cv2.INTER_LINEAR)
cv2.imwrite(output_path,resized_down)
input_path2 = 'test/4.png'
output_path2 = 'test/6.png'
fun2(input_path2,output_path2,ratio=0.5)