How do you manage the photos taken with your mobile (smartphone) camera? In my case, after taking a picture to some extent, I copy it to my PC and erase it from my smartphone so that I try not to leave a large amount of data on my smartphone as much as possible. However, if you delete the photo from your smartphone, the serial number of the file name will be returned, and when you try to copy it to your PC, a file with the same name will be created, which is troublesome.
So, in order to solve the problem of file name fog, I will introduce a method to collectively rename the image files copied to the PC using the shooting date and time.
$ pip3 install Pillow
rename_images.py
from PIL import Image
from PIL.ExifTags import TAGS
from pathlib import Path
import datetime
# https://www.lifewithpython.com/2014/12/python-extract-exif-data-like-data-from-images.html
def get_exif_of_image(file):
"""Get EXIF of an image if exists.
Function to retrieve EXIF data of the specified image
@return exif_table Exif dictionary containing data
"""
im = Image.open(file)
#Get Exif data
#If it does not exist, it ends as it is. Returns an empty dictionary.
try:
exif = im._getexif()
except AttributeError:
return {}
#Since the tag ID itself cannot be read by people, decode it
#Store in table
exif_table = {}
for tag_id, value in exif.items():
tag = TAGS.get(tag_id, tag_id)
exif_table[tag] = value
return exif_table
#Specify the path of the folder on the PC side that contains the image
for filename in Path("/path/to/images").glob("DSC_*.JPG"):
exif = get_exif_of_image(filename)
if "DateTimeOriginal" in exif:
# strftime()Specify the format of the new name with
new_name = Path(filename).with_name(datetime.datetime.strptime(exif["DateTimeOriginal"], "%Y:%m:%d %H:%M:%S").strftime("IMG_%Y%m%d_%H%M%S.JPG"))
print(f"{filename} \n -> {new_name}")
filename.rename(new_name)
else:
print(f"[WARNING] {filename}: no EXIF header")
Extract the shooting date and time from the EXIF header of the image file, create a new file name in the specified format based on the shooting date and time, and rename it.
for filename in Path("/path/to/images").glob("DSC_*.JPG"):The part enumerates the image files contained in a particular folder.``DSC_*.JPG``Please match the part of to the file format generated by the smartphone camera app. This pattern is an example for Xperia.
Use `` get_exif_of_image () `` to get the EXIF header information. This function is taken from the following page. m (_ _) m
[Python Tips: I want to get Exif data of images --Life with Python](https://www.lifewithpython.com/2014/12/python-extract-exif-data-like-data-from-images.html)
Since the shooting date and time is recorded under the name `` DateTimeOriginal``, the date and time character string is parsed by the `` datetime`` module. This recording format should be uniform, so there is no need to customize the `` strptime () `` argument for each camera.
Create a new file name based on this analysis result. Change the argument of `` strftime () `` if you like. However, it's best to avoid names in the same format as the original, such as `` DSC_% Y% m% d_% H% M% S.JPG``.
Click here for the meaning of the format strings of `` strptime () `` and `` strftime () ``.
[8.1. datetime --- Basic Date and Time Types — Python 3.6.12 Documentation](https://docs.python.org/ja/3.6/library/datetime.html#strftime-strptime-behavior)
## Execution example
The log will be displayed as shown below. Make sure the file is renamed correctly.
/path/to/images/DSC_0001.JPG -> /path/to/images/IMG_20190928_135805.JPG /path/to/images/DSC_0004.JPG -> /path/to/images/IMG_20191104_172704.JPG /path/to/images/DSC_0005.JPG -> /path/to/images/IMG_20191106_174423.JPG /path/to/images/DSC_0006.JPG -> /path/to/images/IMG_20191106_174752.JPG /path/to/images/DSC_0007.JPG -> /path/to/images/IMG_20191106_174808.JPG /path/to/images/DSC_0009.JPG -> /path/to/images/IMG_20191213_180056.JPG /path/to/images/DSC_0010.JPG -> /path/to/images/IMG_20191213_180103.JPG /path/to/images/DSC_0011.JPG -> /path/to/images/IMG_20191213_180112.JPG
# Supplement
If possible, I think it's better to rename it on the smartphone side at the time of shooting.
You can use these apps on Android. I will continue to use the app from now on.
[Automatically rename file names when taking photos using "DSC Auto Rename" | Forget-so](http://wasure.net/dscautorename/#i)
Recommended Posts