-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphoto.py
More file actions
85 lines (79 loc) · 3.44 KB
/
photo.py
File metadata and controls
85 lines (79 loc) · 3.44 KB
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
# -*- coding: utf-8 -*-
#转载:https://www.v2ex.com/amp/t/599858/2
import os
import os.path
import shutil
import random
from PIL import Image
from PIL.ExifTags import TAGS
def get_exif_and_arrange_img(imgfile,imgfilename,outdir):
if (not os.path.isdir(outdir)):
os.mkdir(outdir)
try:
img = Image.open(imgfile,"r")
except:
print("not an image file:" + imgfilename)
videodir = outdir + '\\'+ 'VIDEOS'
if (not os.path.isdir(videodir)):
os.mkdir(videodir)
newvideofilename = imgfilename[:imgfilename.rfind('.')] + '_' + str(random.randint(1,999)) + imgfilename[imgfilename.rfind('.'):]
newvideofilename = imgfilename
shutil.copyfile(imgfile,videodir + '\\' + newvideofilename)
else:
try:
exif_data = img._getexif()
except:
print("cannot read exif:" + imgfilename)
otherdir = outdir + '\\'+ 'OTHERS'
if (not os.path.isdir(otherdir)):
os.mkdir(otherdir)
newphotofilename = imgfilename[:imgfilename.rfind('.')] + '_' + str(random.randint(1,999)) + imgfilename[imgfilename.rfind('.'):]
newphotofilename = imgfilename
shutil.copyfile(imgfile,otherdir + '\\' + newphotofilename)
else:
if (exif_data):
device = ''
photodate = ''
fulldate = ''
for tag, value in exif_data.items():
#begin
decoded = TAGS.get(tag,tag)
#print('%s (%s) = %s' % (decoded, tag, value) )
if (decoded == 'Make'):
device += value + '_'
if (decoded == 'Model'):
device += value
if (decoded == 'DateTime'):
photodate = value.replace(':','')[0:6]
fulldate = value.replace(':','')
print(fulldate)
#if (decoded == 'DateTimeDigitized'):
# createdate = value.replace(':','').replace(' ','-')
# print(''+'************'+createdate)
#end
#begin
device = device.replace("\0",'')
#device = device.replace("\32",'')
newfulldate = fulldate.replace(' ','_')
print(imgfile + '---' + device + '---' + photodate + '---' + newfulldate)
#设备名目录
#devicedir = outdir + '\\' + device
#if (not os.path.isdir(devicedir)):
# os.mkdir(devicedir)
#拍照时间
device_datedir = outdir + '\\' + photodate
if (not os.path.isdir(device_datedir)):
os.mkdir(device_datedir)
#文件名
newphotofilename = newfulldate + '_' + str(random.randint(1,9999999)) + imgfilename[imgfilename.rfind('.'):]
newphotofilename = imgfilename
shutil.copyfile(imgfile,device_datedir + '\\' + newphotofilename)
img.close()
#end
rootdir = "D:\\junfile\\1-t\\quen"
outdir = "D:\\junfile\\1-t\\quen\\outdir"
for parent,dirnames,filenames in os.walk(rootdir):
for filename in filenames:
imgfile = os.path.join(parent,filename)
imgfilename = filename
get_exif_and_arrange_img(imgfile,imgfilename,outdir)