# -*- coding: utf-8 -*-
# @Time : 2021/9/25 19:11
# @Author : Xiong wenwei
# @Email : 184859498@qq.com
# @File : test.py
# Desc :
import os
import time
import shutil
import exifread
def get_file_name(dir):
"""
获取当前文件夹下的所有文件列表
"""
temp = []
for files in os.listdir(dir):
if os.path.isfile(os.path.join(dir, files)): #判断是否是文件?
temp.append(files)
return temp
def get_file_dt(file_name):
"""
获取文件修改的年月份,如:2019.09
"""
ctime = os.path.getmtime(file_name)
timestruct = time.localtime(ctime)
file_create_time = time.strftime('%Y.%m', timestruct)
return(file_create_time)
def get_file_dt(dir):
"""
获取文件修改的年月份,如:2019.09
"""
ctime = os.path.getmtime(dir)
timestruct = time.localtime(ctime)
file_create_time = time.strftime('%Y.%m', timestruct)
return(file_create_time)
# 获取照片位置、拍摄日期
class Image_Location():
def __init__(self, image_path):
self.img_path = image_path
def lati_long_date(self, data):
"""
对经纬度进行处理,保留6位小数
"""
# 删除左右括号,最后以逗号分隔为一个列表
data_list_tmp = str(data) .replace('[', '').replace(']', '').split(',')
# 循环取出每个元素,删除元素两边的空格,得到一个新列表
data_list = [date.strip() for date in data_list_tmp]
# 替换秒的值
data_tmp = data_list[-1].split('/')
# 秒的值
data_sec = int(data_tmp[0]) / int(data_tmp[1]) / 3600
# 替换分的值
data_tmp = data_list[-2]
# 分的值
data_minute = int(data_tmp) / 60
# 度的值
data_degree = int(data_list[0])
# 由于高德API只能识别到小数点后的6位
# 需要转换为浮点数,并保留为6位小数
result = "%.6f" % (data_degree + data_minute + data_sec)
return float(result)
def get_image_exif(self):
"""
获取图片的属性:纬度,经度,拍摄时间等
"""
# 通过exifread获取图片的属性
img_read = open(self.img_path, 'rb')
img_exif = exifread.process_file(img_read)
# 读取到的属性
if img_exif:
for exif in img_exif:
# 纬度
if exif == "GPS GPSLatitude":
latitude = img_exif["GPS GPSLatitude"]
# 纬度的方向
elif exif == "GPS GPSLatitudeRef":
latitude_direction = img_exif["GPS GPSLatitudeRef"]
# 经度
elif exif == "GPS GPSLongitude":
longitude = img_exif["GPS GPSLongitude"]
# 经度方向
elif exif == "GPS GPSLongitudeRef":
longitude_direction = img_exif["GPS GPSLongitudeRef"]
# 拍摄时间
elif exif == "EXIF DateTimeDigitized":
take_time = img_exif["EXIF DateTimeDigitized"]
# 拍摄设备
elif exif == "Image Make":
take_equipment = img_exif["Image Make"]
# 拍摄型号
elif exif == "Image Model":
take_model = img_exif["Image Model"]
print('-'*120)
res1 = 'take_equipment' in dir()
if res1:
print("拍摄设备为: ", take_equipment)
res2 = 'take_model' in dir()
if res2:
print("拍摄型号为:", take_model)
res3 = 'take_time' in dir()
if res3:
print("拍摄时间为:", take_time)
dt_dir = str(take_time)[0:7].replace(':', '.')
print(dt_dir)
if 'dt_dir' in dir():
return dt_dir
else:
print("图像信息为空,可能是上传的不是原图")
return False
if __name__ == '__main__':
path = r'F:\2020_照片统一备份'
file_list = get_file_name(path)
# 按照年月创建文件夹
create_dir = []
photos = []
for i in file_list:
file_name = os.path.join(path, i)
print(file_name)
location = Image_Location(file_name)
dir_dt = location.get_image_exif()
if dir_dt != False and dir_dt is not None:
create_dir.append(dir_dt)
photos.append(i)
print("%d个月:%s" % (len(set(create_dir)), set(create_dir)))
print(photos)
for j in set(create_dir):
temp = os.path.join(path, j)
if os.path.exists(temp):
continue
os.mkdir(temp)
# 仅复制获取到拍摄日期的文件到指定文件夹
move_counter = 0
for file_name in photos:
source = os.path.join(path, file_name)
location = Image_Location(source)
dest_dt = location.get_image_exif()
dest_path = os.path.join(path, dest_dt)
dest = os.path.join(dest_path, file_name)
print('正在将 %s 移动到 %s' % (file_name, dest_path))
shutil.move(source, dest)
move_counter += 1
print('共有%d个图片文件' % len(file_list))
print('总共移动了 %d 个文件' % move_counter)
文章评论