1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
import os import io import re import time import shutil import exifread import whatimage import pyheif import piexif from PIL import Image from types import coroutine
def heic_to_jpg(heic_img_path): with open(heic_img_path, 'rb') as f: heic_img = f.read()
img_format = whatimage.identify_image(heic_img) print('heic_to_jpg', img_format) if img_format in ['heic', 'heif']: img = pyheif.read_heif(heic_img) pi = Image.frombytes(mode=img.mode, size=img.size, data=img.data) pi.save(heic_img_path[:-5]+".jpg", format="jpeg")
def get_file_date(filepath, format): ''' 获取文件的时间戳,这里主要返回视频的时间,原时间一般为修改时间 st_atime (访问时间), st_mtime (修改时间), st_ctime(创建时间) ''' statinfo = os.stat(filepath) temp_time = time.localtime(statinfo.st_mtime) return time.strftime(format, temp_time)
def get_exif_date(filepath): FIELD = 'EXIF DateTimeOriginal' fd = open(filepath, 'rb') tags = exifread.process_file(fd) fd.close()
if FIELD in tags: temp_time = str(tags[FIELD]) new_time = temp_time.replace(':', '').replace(' ', '_') return new_time else: new_time = '' return new_time
def get_heic_exif_date(filepath): with open(filepath, 'rb') as f: heic_img = f.read()
img_format = whatimage.identify_image(heic_img) temp_time = "" print('heic_to_jpg', img_format) if img_format in ['heic', 'heif']: img = pyheif.read_heif(heic_img) for metadata in img.metadata or []: if metadata['type'] == 'Exif': FIELD = 36867 exif_dict = piexif.load(metadata['data']) exif_dict = exif_dict['Exif'] temp_time = (exif_dict[FIELD]).decode('utf-8') print("时间: " + temp_time)
new_time = temp_time.replace(':', '').replace(' ', '_') return new_time
def get_filetype(basename): ''' 利用正则表达式判断文件后缀,照片返回0,视频返回1,其他返回2 ''' img_reg = r'(\.JPG|\.jpg|\.JPEG|\.jpeg|\.BMP|\.bmp|\.PNG|\.png)' vedio_reg = r'(\.MP4|\.mp4|\.MOV|\.mov)'
if re.search(img_reg, basename): return 0 elif re.search(vedio_reg, basename): return 1 else: if re.search(r'(\.HEIC|\.heic|\.HEIF|\.heif)', basename): return 2 else: return 3
def rename_filename(path, filename, format): filepath = os.path.join(path, file_name) newname = filename print("当前文件: " + filepath) if filename == ".DS_Store": return
num = get_filetype(filename) if num == 0: exif_time = str(get_exif_date(filepath)) if exif_time != '': str_time = exif_time else: str_time = str(get_file_date(filepath, format)) elif num == 1: str_time = str(get_file_date(filepath, format)) elif num == 2: exif_time = str(get_heic_exif_date(filepath)) if exif_time != '': str_time = exif_time else: str_time = str(get_file_date(filepath, format)) else: print("- 文件格式不正确!") return newname
file_suffix = os.path.splitext(filename)[1] try: newname = str_time+file_suffix os.rename(filepath, os.path.join(path, newname)) print(filename + " newname: " + newname + ' 重命名成功!') except Exception as e: if e.args[0] == 17: newname = str_time+'-1'+file_suffix os.rename(filepath, os.path.join(path, newname)) else: print(e) return newname
def move_file(path, file_name, todir): if file_name == None: return oldpath = os.path.join(path, file_name) new_dir = os.path.join(todir, "/Other") try: new_dir = os.path.join(todir, file_name[:4] + "/" + file_name[4: 6]) except Exception as e: print("Exception: " + e)
if not os.path.exists(new_dir): os.makedirs(new_dir) newpath = os.path.join(new_dir, file_name) shutil.move(oldpath, newpath) print("移动文件:" + newpath)
def simple_move_file(path, file_name, todir): if file_name == ".DS_Store": return else: shutil.move(os.path.join(path, file_name), os.path.join(todir, file_name))
def get_allsubdir(path): print("所有子目录: ") g = os.walk(path) for path, dir_list, file_list in g: for dir_name in dir_list: print(os.path.join(path, dir_name))
def get_allfiles(path): print("所有文件: ") g = os.walk(path) for path, dir_list, file_list in g: for file_name in file_list: print(os.path.join(path, file_name))
if __name__ == "__main__": fromdir = '/Users/vvusu/Downloads' todir = '/Volumes/homes/suixin/Photos/MobileBackup/Sue/Other' format = '%Y%m%d_%H%M%S' print("时间格式: " + format) g = os.walk(fromdir) print("所有文件: ") for path, dir_list, file_list in g: for file_name in file_list: new_name = rename_filename(path, file_name, format)
|