掃描儀掃描文件處理之scan_remove_image_exif.py
阿新 • • 發佈:2017-07-21
new ase mtime main方法 %d utf-8 oat 最大 pil
# -*- coding: utf-8 -*- # version: python 3 # ========== # 作用: # 刪除圖片的exif信息 # ========== # pip3 install Pillow # 當停止腳本後再次運行會刪除最後生成的5個文件(按最修改時間排序),已經有的文件跳過。 # ========== import sys, os import time from PIL import Image path = ‘/Users/osx/Desktop/test‘ # 處理目錄【修改】 suffix = ‘jpg‘ # "處理目錄"中的指定圖片後綴【修改】 out_path = os.path.join(path, ‘no-exif‘) # 輸出目錄 # if os.path.exists(out_path): # print(‘輸出目錄已存在,請移走後再運行程序!‘) # sys.exit() if not os.path.exists(out_path): os.makedirs(out_path) def get_file_list(file_list_path, file_list_suffix): """得到指定後綴的文件列表""" exclude = ([‘.DS_Store‘, ‘.localized‘, ‘Thumbs.db‘, ‘desktop.ini‘]) result_list = [] if os.path.isfile(file_list_path): result_list.append(os.path.abspath(file_list_path)) else: for dir_path, dir_names, file_names in os.walk(file_list_path): if os.path.abspath(dir_path) != os.path.abspath(file_list_path): # 只允許 1 層目錄 continue for name in file_names: if (not os.path.basename(name) in exclude) and (os.path.splitext(name)[1][1:] == file_list_suffix): # 指定後綴 abs_path = os.path.abspath(os.path.join(dir_path, name)) result_list.append(abs_path) return result_list def parse_image(in_image_file, out_image_file): """ 刪除圖片exif信息 """ # -----刪除exif信息----- image_file = open(in_image_file, ‘rb‘) image = Image.open(image_file) data = list(image.getdata()) image_without_exif = Image.new(image.mode, image.size) image_without_exif.putdata(data) image_without_exif.save(out_image_file) def resume(): """刪除最後處理的5個圖片(按最修改時間排序)""" file_list = get_file_list(out_path, suffix) new_file_list = sorted(file_list, key=os.path.getmtime, reverse=True) i = 0 for new_tar in new_file_list: if i >= 5: break print("mtime: %s delete: %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getmtime(new_tar)) ), new_tar)) os.remove(new_tar) i += 1 def analyse(in_reverse): """打印最大、最小的5個文件""" file_list = get_file_list(out_path, suffix) new_file_list = sorted(file_list, key=os.path.getsize, reverse=in_reverse) i = 0 for new_tar in new_file_list: if i >= 5: break print("size(Kilobyte): %s" % (round(os.path.getsize(new_tar) / float(1024)))) i += 1 def main(): """主方法/main方法""" count = 0 file_list = get_file_list(path, suffix) for tar in file_list: tar_name = os.path.basename(tar) tar_out = os.path.join(out_path, tar_name) # 跳過已有文件 if os.path.exists(tar_out): continue count += 1 print(‘%s %s‘ % (count, tar_name)) parse_image(tar, tar_out) # 處理圖片 print(‘----------‘) print(‘總共處理了:%s‘ % (count)) print(‘max --> min‘) analyse(in_reverse=True) print(‘----------‘) print(‘min --> max‘) analyse(in_reverse=False) print(‘----------‘) resume() print(‘----------‘) main()
掃描儀掃描文件處理之scan_remove_image_exif.py