方法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在遇到过大的图片可能无法转换

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐