-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequirement3.py
83 lines (69 loc) · 2.76 KB
/
requirement3.py
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
# -*- coding: utf-8 -*-
# coding=gbk
import exifread
import os
import natsort
import tkinter
import tkinter.messagebox
import time
#
# pic_path='C:/Users/Mr.Chow/Desktop/test/picture'
# num = 0
def exif_image(filename,mode):
"""
:param filename: #文件地址
:param mode: #文件模式
:return:
"""
global num
with open(filename, 'rb') as f:
tags = exifread.process_file(f)
# if re.match('Image Make', tag):
# print('[*] 品牌信息: ' + str(value))
# if re.match('Image Model', tag):
# print('[*] 具体型号: ' + str(value))
# if re.match('EXIF LensModel', tag):
# print('[*] 摄像头信息: ' + str(value))
# if re.match('EXIF DateTimeOriginal', tag):
pic_path = os.path.split(filename)[0]
if mode=="品牌": #品牌
ImageModel = str(tags['Image Model'])
new_name=ImageModel+'0'+ str(num)+os.path.splitext(filename)[1]
new_name = pic_path + '/' + new_name
elif mode=="拍摄时间": #照片时间
if "EXIF DateTimeOriginal" in tags:
print(tags['EXIF DateTimeOriginal'])
DateTime = str(tags['EXIF DateTimeOriginal'])
new_name = DateTime.replace(':', '').replace(' ', '_') + '0' + str(num) + os.path.splitext(filename)[1]
new_name = pic_path + '/' + new_name
elif mode=="作者信息": #作者信息
ImageArtist = str(tags['Image Artist'])
new_name=ImageArtist +os.path.splitext(filename)[1]
new_name = pic_path + '/' + new_name
else: #系统顺序
new_name=str(num)+os.path.splitext(filename)[1]
new_name = pic_path + '/' + new_name
num+=1
os.rename(filename, new_name)
def rename(pic_path,i):
global num
num=0
root = tkinter.Tk()
root.withdraw() # 隐藏主窗口
root.wm_attributes('-topmost', 1) # 消息框置顶
for filename in natsort.natsorted(os.listdir(pic_path)): #遍历地址下文件名
filename=pic_path+'/'+filename #获得文件路径
print(filename)
if os.path.isfile(filename):
try:
exif_image(filename,i)
except FileExistsError:
tkinter.messagebox.showinfo("提示","文件已存在 无法重命名 请删除后再试")
break
except KeyError:
tkinter.messagebox.showinfo("没有关键信息 无法重命名")
break
root.destroy()
if __name__=='__main__':
pic_path = 'C:/Users/Mr.Chow/Desktop/test/picture'
rename(pic_path,4)