Python PIL save file with datetime as name
In this article, we are going to see how to save image files with datetime as a name using PIL Python.
PIL: This library provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities.
pip install Pillow
datetime: This module helps us to work with dates and times in Python.
pip install datetime
os: This module provides a portable way of using operating system-dependent functionality. The *os* and *os.path* modules include many functions to interact with the file system.
Step 1: Open the image using Image module with the provided path.
img = Image.open(path)
curr_datetime = datetime.now().strftime('%Y-%m-%d %H-%M-%S')
Step 3: Split the path using os.path.splitext(path) into root and extension.
splitted_path = os.path.splitext(picture_path)
Step 4: Add the current datetime in between root and extension and concatenate them.
modified_picture_path = splitted_path + curr_datetime + splitted_path
Step 5: Save the image with the modified path using Image module.
Below is the full implementation:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course