按比例改变图片大小

这篇博客介绍了三种不同的Python库——skimage、PIL和opencv,用于实现图像的缩放操作。方法一利用skimage库,通过指定图片比例进行缩放;方法二仅使用PIL库,提供了多种插值方式;方法三采用opencv2,利用INTER_LINEAR插值。这些方法在处理图像尺寸调整时各有特点。
摘要由CSDN通过智能技术生成

法一:
使用的是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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值