1. 程式人生 > 程式設計 >python 無損批量壓縮圖片(支援保留圖片資訊)的示例

python 無損批量壓縮圖片(支援保留圖片資訊)的示例

由於雲盤空間有限,照片尺寸也是很大,所以寫個Python程式壓縮一下照片,騰出一些雲盤空間

1、批量壓縮照片

新建 photo_compress.py 程式碼如下

# -*- coding: utf-8 -*-

"""指令碼功能說明:使用 tinypng api,一鍵批量壓縮指定檔案(夾)所有檔案"""

import os
import sys
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor # 執行緒池,程序池
import json
import random
import requests
from you_get import common
from shutil import copyfile


def 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 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,output_path)
  # download_thread_pool.submit(download_tinypng,file_path,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'報錯了:%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個執行緒執行此任務
 download_thread_pool = ThreadPoolExecutor(10) # 定義5個執行緒執行此任務
 process_pool = ProcessPoolExecutor(8) # 定義5個程序
 len_param = len(sys.argv)
 if len_param != 2 and len_param != 3:
  print('請使用: %s [filepath]' % os.path.basename(sys.argv[0]))
 else:
  check_path(sys.argv[1])
  input("Press <enter> 請耐心等待\n")

執行python .\photo_compress.py F:\\test

python 無損批量壓縮圖片(支援保留圖片資訊)的示例

生成compress_output資料夾,裡面就是壓縮的檔案,但此時的照片沒有,拍攝時的時間、位置的資訊,所以下面要複製檔案資訊

若要壓縮的檔案不全,可以再執行一次壓縮(會自動過濾已壓縮的照片)

2、批量拷貝照片資訊

使用pyexiv2進行檔案資訊拷貝

pip install pyexiv2 -i https://pypi.tuna.tsinghua.edu.cn/simple

新建 copy_fileinfo.py 程式碼如下

# -*- coding: utf-8 -*-

"""指令碼功能說明:使用 pyexiv2 api,一鍵批量拷貝指定檔案(夾)所有檔案資訊"""

import os
import sys
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor # 執行緒池,程序池
from pyexiv2 import Image


def 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 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),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"已跳過拷貝檔案資訊:",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")

 # 方向不拷貝,防止圖片旋轉
 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,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,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 無損批量壓縮圖片的資料請關注我們其它相關文章!