栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 降低图片分辨率的两种方法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 降低图片分辨率的两种方法

方法1:

# 降低图片分辨率
def ResizeImage(filein, fileout,width,height, scale=1):
    """
    改变图片大小
    :param filein: 输入图片的文件夹路径
    :param fileout: 输出图片的文件夹路径
    :param width: 输出图片宽度
    :param height: 输出图片宽度
    :param type: 输出图片类型(png, gif, jpeg...)
    :return:
    """
    allImage = os.listdir(filein)
    for image in allImage:
        img = Image.open(filein+"/"+image)
        type = img.format
        out = img.resize((int(width), int(height)), Image.ANTIALIAS)
        # 第二个参数:
        # Image.NEAREST :低质量
        # Image.BILINEAR:双线性
        # Image.BICUBIC :三次样条插值
        # Image.ANTIALIAS:高质量
        out.save(fileout+"/"+image, type)

方法2:

def change_size(input_path: str, output_dir: str, width: int, height: int):
    allImage = os.listdir(input_path)
    for image in allImage:
        try:
            image_path = input_path+"/"+image
            ext = os.path.basename(image_path).strip().split('.')[-1]
            if ext not in ['png', 'jpg']:
                raise Exception('format error')
            _result_path = os.path.join(output_dir, '{}.{}'.format(image[:-4], ext))
            ff = FFmpeg(executable='D:/czc/ffmpeg-4.2-win-64/ffmpeg.exe',inputs={'{}'.format(image_path): None}, outputs={_result_path: '-vf scale={}:{}'.format(width, height)})
            print(ff.cmd)
            ff.run()
            return _result_path
        except:
            print(image+"已转换")



if __name__ == '__main__':
    png_path = D:/czc/png
    smallPng_path = D:/czc/smallPng
    width = 1280
    height = 720
    change_size(png_path,smallPng_path,width,height)

 方法2在遇到过大的图片可能无法转换

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/915882.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号