python 无损批量压缩图片(支持保留图片信息)的示例

时间:2021-05-22

由于云盘空间有限,照片尺寸也是很大,所以写个Python程序压缩一下照片,腾出一些云盘空间

1、批量压缩照片

新建 photo_compress.py 代码如下

# -*- coding: utf-8 -*-"""脚本功能说明:使用 tinypng api,一键批量压缩指定文件(夹)所有文件"""import osimport sysfrom concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor # 线程池,进程池import jsonimport randomimport requestsfrom you_get import commonfrom shutil import copyfiledef get_file_dir(file): """获取文件目录通用函数""" fullpath = os.path.abspath(os.path.realpath(file)) return os.path.dirname(fullpath)def check_suffix(file_path): """检查指定文件的后缀是否符合要求""" file_path_lower = file_path.lower() return (file_path_lower.endswith('.png') or file_path_lower.endswith('.jpg') or file_path_lower.endswith('.jpeg'))def download_tinypng(input_file, url, output_file): file_name = os.path.basename(input_file) arr = file_name.split('.') new_file_name = arr[len(arr) - 2] + '_compress' new_output_file = os.path.join(os.path.dirname(output_file), arr[len(arr) - 2] + '_compress.' + arr[len(arr) - 1]) print(u'开始下载文件 :%s' % new_output_file) # print(os.path.splitext(os.path.basename(output_file))[0]) sys.argv = ['you-get', '-o', os.path.dirname( output_file), '-O', new_file_name, url] common.main() old_size = os.path.getsize(input_file) new_size = os.path.getsize(new_output_file) print(u'文件保存地址:%s' % new_output_file) print(u'压缩后文件大小:%d KB' % (new_size / 1024)) print(u'压缩比: %d%%' % ((old_size - new_size) * 100 / old_size))def compress_by_tinypng(input_file): if not check_suffix(input_file): print(u'只支持png\\jpg\\jepg格式文件:' + input_file) return file_name = os.path.basename(input_file) arr = file_name.split('.') new_file_name = arr[len(arr) - 2] + '_compress.' + arr[len(arr) - 1] output_path = os.path.join(get_file_dir(input_file), 'compress_output') output_file = os.path.join(output_path, new_file_name) if not os.path.isdir(output_path): os.makedirs(output_path) if (os.path.exists(output_file)): print("已存在,跳过压缩") return try: old_size = os.path.getsize(input_file) print(u'压缩前文件名:%s文件大小:%d KB' % (input_file, old_size / 1024)) if (old_size < 1024 * 1024): print("已跳过压缩,并直接拷贝文件") try: copyfile(input_file, output_file) except IOError as e: print("Unable to copy file. %s" % e) return print("开始压缩") shrink_image(input_file) print(u'文件压缩成功:%s' % input_file) # download_thread_pool.submit(download_tinypng, source, input_file, output_file) except Exception as e: print(u'报错了:%s' % e)def check_path(input_path): """如果输入的是文件则直接压缩,如果是文件夹则先遍历""" if os.path.isfile(input_path): compress_by_tinypng(input_path) elif os.path.isdir(input_path): dirlist = os.walk(input_path) for root, dirs, files in dirlist: if (not (root.endswith("\\compress_output") or root.endswith("/compress_output"))): i = 0 for filename in files: i = i + 1 process_pool.submit(compress_by_tinypng, os.path.join( root, filename)) # compress_by_tinypng(os.path.join(root, filename)) else: print(u'目标文件(夹)不存在,请确认后重试。')def list_images(path): images = None try: if path: os.chdir(path) full_path = os.getcwd() files = os.listdir(full_path) images = [] for file in files: ext = os.path.splitext(file)[1].lower() if ext in ('.jpg', '.jpeg', '.png'): images.append(os.path.join(full_path, file)) except: pass return imagesdef shrink_image(file_path): print(u'源文件地址:%s' % file_path) result = shrink(file_path) if result: output_path = generate_output_path(file_path) url = result['output']['url'] print(u'下载地址:%s' % url) download_tinypng(file_path, url, output_path) # download_thread_pool.submit(download_tinypng, file_path, url, output_path) # response = requests.get(url) # with open(output_path, 'wb') as file: # file.write(response.content) # print(u'文件保存地址:%s' % output_path) # print('%s %d=>%d(%f)' % ( # result['input']['type'], # result['input']['size'], # result['output']['size'], # result['output']['ratio'] # )) else: print('压缩失败')def shrink(file_path): url = 'https://tinypng.com/web/shrink' headers = { 'Cache-Control': 'no-cache', 'Content-Type': 'application/x-press_output"))): i = 0 for filename in files: i = i + 1 process_pool.submit(copyinfo_by_pyexiv2, os.path.join( root, filename)) else: print(u'目标文件(夹)不存在,请确认后重试。')if __name__ == '__main__': # thread_pool = ThreadPoolExecutor(10) # 定义5个线程执行此任务 process_pool = ProcessPoolExecutor(8) # 定义5个进程 len_param = len(sys.argv) if len_param != 2: print('请使用: %s [filepath]' % os.path.basename(sys.argv[0])) else: check_path(sys.argv[1]) input("Press <enter> 请耐心等待\n")

执行python .\copy_fileinfo.py F:\\test

大功告成!图片压缩完毕,信息还没有丢失

以上就是python 无损批量压缩图片(支持保留图片信息)的示例的详细内容,更多关于python 无损批量压缩图片的资料请关注其它相关文章!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章