patternpythonMinor
Renaming picture files with date
Viewed 0 times
picturewithdatefilesrenaming
Problem
I am wondering if this is a safe and/or sensible way of going about doing the following task:
My problem is that I have a folder with loads of directories with names of form
Here is my current solution. I tested it so that it also doesn't keep adding the date each time I run the code - once the files have been renamed they are not renamed in the future.
Am I safe running this code? By safe, I mean I don't end up with a renaming catastrophe. Also, please feel free to comment on the efficiency and style of my code.
My problem is that I have a folder with loads of directories with names of form
YYYY-MM-DD. They are from my camera. Each directory contains picture files. The problem is that the picture files are designated by just 4 digits like C4018.jpeg. Hence after a few days of shooting I will find myself with duplicate file names! My objective is then to just add the date of the containing folder to each file, which solves my problem.Here is my current solution. I tested it so that it also doesn't keep adding the date each time I run the code - once the files have been renamed they are not renamed in the future.
Am I safe running this code? By safe, I mean I don't end up with a renaming catastrophe. Also, please feel free to comment on the efficiency and style of my code.
import os
import re
rootdir = '/Volumes/Misc/Pictures/test/' #Location of video folders to rename
# Regexp for YYYY-MM-DD or YYYY/MM/DD
date_re = re.compile('\d{4}[-/]\d{2}[-/]\d{2}')
subdirs = [subdir[0] for subdir in os.walk(rootdir)]
for subdir in subdirs:
match = date_re.findall(subdir)
if match:
os.chdir(subdir)
for (_,_,files) in os.walk(os.getcwd()):
for f in files:
match2 = date_re.findall(f)
if not match2:
new_name = match[0] + '-' + f
os.rename(f, new_name)
os.chdir('..')Solution
Why don't you use the exif timestamps of your images and name them accordingly? I use the following code to rename my images. It also extends the timestamp to feature not only the date but also the time:
If you want to use your own code, why don't you just test it on a copy of the data?
import os
import glob
from PIL import Image
from PIL.ExifTags import TAGS
import time
def get_exif(fn):
ret = {}
i = Image.open(fn)
info = i._getexif()
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
return ret
os.chdir("/path/to/images")
files = glob.glob("*.JPG")
for file in files:
print(file)
time = get_exif(file)["DateTimeOriginal"]
time = time.replace(":", "")
time = time.replace(" ", "_")
number = 0
new_name = time+"_additional_information.jpg"
if new_name == file:
print(new_name, "already ok")
continue
while os.path.exists(new_name):
number += 1
new_name = time+"_"+str(number)+"_additional_information.jpg"
os.rename(file, new_name)If you want to use your own code, why don't you just test it on a copy of the data?
Code Snippets
import os
import glob
from PIL import Image
from PIL.ExifTags import TAGS
import time
def get_exif(fn):
ret = {}
i = Image.open(fn)
info = i._getexif()
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
return ret
os.chdir("/path/to/images")
files = glob.glob("*.JPG")
for file in files:
print(file)
time = get_exif(file)["DateTimeOriginal"]
time = time.replace(":", "")
time = time.replace(" ", "_")
number = 0
new_name = time+"_additional_information.jpg"
if new_name == file:
print(new_name, "already ok")
continue
while os.path.exists(new_name):
number += 1
new_name = time+"_"+str(number)+"_additional_information.jpg"
os.rename(file, new_name)Context
StackExchange Code Review Q#113683, answer score: 4
Revisions (0)
No revisions yet.