python 無(wú)損批量壓縮圖片(支持保留圖片信息)的示例
由于云盤(pán)空間有限,照片尺寸也是很大,所以寫(xiě)個(gè)Python程序壓縮一下照片,騰出一些云盤(pán)空間
1、批量壓縮照片
新建 photo_compress.py 代碼如下
# -*- coding: utf-8 -*- """腳本功能說(shuō)明:使用 tinypng api,一鍵批量壓縮指定文件(夾)所有文件""" import os import sys from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor # 線程池,進(jìn)程池 import json import random import requests from you_get import common from shutil import copyfile def get_file_dir(file): """獲取文件目錄通用函數(shù)""" 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'開(kāi)始下載文件 :%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("已存在,跳過(guò)壓縮") 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("已跳過(guò)壓縮,并直接拷貝文件") try: copyfile(input_file, output_file) except IOError as e: print("Unable to copy file. %s" % e) return print("開(kāi)始?jí)嚎s") 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'報(bào)錯(cuò)了:%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'目標(biāo)文件(夾)不存在,請(qǐng)確認(rèn)后重試。') 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 images def 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-www-form-urlencoded', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36 Edg/85.0.564.44', 'X-Forwarded-For': get_random_ip() } result = None try: file = open(file_path, 'rb') response = requests.post(url, headers=headers, data=file) result = json.loads(response.text) except Exception as e: print(u'報(bào)錯(cuò)了:%s' % e) if file: file.close() if result and result['input'] and result['output']: return result else: return None def generate_output_path(file_path): parent_path = os.path.abspath(os.path.dirname(file_path)) output_path = os.path.join(parent_path, 'compress_output') if not os.path.isdir(output_path): os.mkdir(output_path) return os.path.join(output_path, os.path.basename(file_path)) def get_random_ip(): ip = [] for i in range(4): ip.append(str(random.randint(0 if i in (2, 3) else 1, 254))) return '.'.join(ip) if __name__ == '__main__': thread_pool = ThreadPoolExecutor(5) # 定義5個(gè)線程執(zhí)行此任務(wù) download_thread_pool = ThreadPoolExecutor(10) # 定義5個(gè)線程執(zhí)行此任務(wù) process_pool = ProcessPoolExecutor(8) # 定義5個(gè)進(jìn)程 len_param = len(sys.argv) if len_param != 2 and len_param != 3: print('請(qǐng)使用: %s [filepath]' % os.path.basename(sys.argv[0])) else: check_path(sys.argv[1]) input("Press <enter> 請(qǐng)耐心等待\n")
執(zhí)行python .\photo_compress.py F:\\test
生成compress_output文件夾,里面就是壓縮的文件,但此時(shí)的照片沒(méi)有,拍攝時(shí)的時(shí)間、位置的信息,所以下面要復(fù)制文件信息
若要壓縮的文件不全,可以再執(zhí)行一次壓縮(會(huì)自動(dòng)過(guò)濾已壓縮的照片)
2、批量拷貝照片信息
使用pyexiv2進(jìn)行文件信息拷貝
pip install pyexiv2 -i https://pypi.tuna.tsinghua.edu.cn/simple
新建 copy_fileinfo.py 代碼如下
# -*- coding: utf-8 -*- """腳本功能說(shuō)明:使用 pyexiv2 api,一鍵批量拷貝指定文件(夾)所有文件信息""" import os import sys from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor # 線程池,進(jìn)程池 from pyexiv2 import Image def get_file_dir(file): """獲取文件目錄通用函數(shù)""" 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 copyinfo_by_pyexiv2(input_file): 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 (check_suffix(input_file) or check_suffix(output_file)): print(u'只支持png\\jpg\\jepg格式文件:' + input_file) return if not (os.path.exists(output_file)): print(u'文件不存在:' + output_file) return old_size = os.path.getsize(input_file) if (old_size < 1024 * 1024): print(u"已跳過(guò)拷貝文件信息:", input_file) return # if not os.path.isdir(output_path): # os.makedirs(output_path) try: i = Image(input_file) # 源圖片路徑 except Exception: i = Image(input_file, "GB18030") try: _exif_info = i.read_exif() except Exception: _exif_info = i.read_exif("GB18030") # print(_exif_info) # _iptc_info = i.read_iptc() # print(_iptc_info) # _xmp_info = i.read_xmp() # print(_xmp_info) i.close() try: i2 = Image(output_file) # 拷貝信息圖片路徑 except Exception: i2 = Image(output_file, "GB18030") try: _exif_info2 = i2.read_exif() except Exception: _exif_info2 = i2.read_exif("GB18030") # 方向不拷貝,防止圖片旋轉(zhuǎn) for item in _exif_info: if("Exif.Image.Orientation" != item): if (_exif_info2.get(item) != _exif_info.get(item)): try: i2.modify_exif({item: _exif_info[item]}) except Exception as e: print(e) try: i2.modify_exif({item: _exif_info[item]}, "GB18030") except Exception as e: print(e) i2.close() print(u"拷貝信息完成:" + input_file) def check_path(input_path): """如果輸入的是文件則直接壓縮,如果是文件夾則先遍歷""" if os.path.isfile(input_path): copyinfo_by_pyexiv2(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(copyinfo_by_pyexiv2, os.path.join( root, filename)) else: print(u'目標(biāo)文件(夾)不存在,請(qǐng)確認(rèn)后重試。') if __name__ == '__main__': # thread_pool = ThreadPoolExecutor(10) # 定義5個(gè)線程執(zhí)行此任務(wù) process_pool = ProcessPoolExecutor(8) # 定義5個(gè)進(jìn)程 len_param = len(sys.argv) if len_param != 2: print('請(qǐng)使用: %s [filepath]' % os.path.basename(sys.argv[0])) else: check_path(sys.argv[1]) input("Press <enter> 請(qǐng)耐心等待\n")
執(zhí)行python .\copy_fileinfo.py F:\\test
大功告成!圖片壓縮完畢,信息還沒(méi)有丟失
以上就是python 無(wú)損批量壓縮圖片(支持保留圖片信息)的示例的詳細(xì)內(nèi)容,更多關(guān)于python 無(wú)損批量壓縮圖片的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python-opencv在有噪音的情況下提取圖像的輪廓實(shí)例
下面小編就為大家?guī)?lái)一篇python-opencv在有噪音的情況下提取圖像的輪廓實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08PyCharm使用之配置SSH Interpreter的方法步驟
這篇文章主要介紹了PyCharm使用之配置SSH Interpreter的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12python通過(guò)txt文件批量安裝依賴(lài)包的實(shí)現(xiàn)步驟
今天小編就為大家分享一篇python通過(guò)txt文件批量安裝依賴(lài)包的實(shí)現(xiàn)步驟,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Python+?Flask實(shí)現(xiàn)Mock?Server詳情
這篇文章主要介紹了Python+?Flask實(shí)現(xiàn)Mock?Server詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09matplotlib一維散點(diǎn)分布圖的實(shí)現(xiàn)
本文主要介紹了matplotlib一維散點(diǎn)分布圖的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03深入理解python Matplotlib庫(kù)的高級(jí)特性
Matplotlib是一款極其強(qiáng)大的Python數(shù)據(jù)可視化庫(kù),這篇文章中,我們將深入討論 Matplotlib 的一些高級(jí)特性,包括對(duì)象導(dǎo)向接口、自定義顏色映射和樣式、動(dòng)態(tài)圖形等,感興趣的小伙伴跟著小編一起來(lái)探討吧2023-07-07python實(shí)現(xiàn)跳表SkipList的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)跳表SkipList的示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07簡(jiǎn)單了解python單例模式的幾種寫(xiě)法
這篇文章主要介紹了簡(jiǎn)單了解python單例模式的幾種寫(xiě)法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07