python压缩图片
# 需要安装pillow库 import os from PIL import Image def compress(src_path, dst_path, compress_rate=0.5): img = Image.open(src_path) w = int(img.size[0] * compress_rate) h = int(img.size[1] * compress_rate) img_resize = img.resize((w, h)) img_resize.save(dst_path) if __name__ == "__main__": # 如果文件夹相同,则会直接替换 src_dir = r'C:\Users\ming\Desktop\src' dst_dir = r'C:\Users\ming\Desktop\des' if not os.path.exists(dst_dir): os.mkdir(dst_dir) for filename in os.listdir(src_dir): img_path = os.path.join(src_dir, filename) new_img_path = os.path.join(dst_dir, filename) compress(img_path, new_img_path, 0.5)
如上例子,把桌面的src文件夹下所有图片,宽高压缩50%,保存到桌面des文件夹下。