Function to save images by date [python3]

I think it can get messy when saving images in python. If you use the following function, the image will be saved by dividing the path for each day.

Example: In the case of 2020/10/27 (10:15:30)

~/(Execution directory)/2020/10/27/10_15_30.png
from datetime import datetime
import os


def save_image(img):

    #Save image
       #Create folders for the year and month
    nowtime = datetime.now()
    savedir = os.getcwd()


    if not os.path.exists(os.path.join(savedir)):
        os.mkdir(os.path.join(savedir))
        print("MAKE_DIR: " + savedir)
    #Create year folder
    savedir += datetime.now().strftime("/%Y")
    if not os.path.exists(os.path.join(savedir)):
        os.mkdir(os.path.join(savedir))
        print("MAKE_DIR: " + savedir)
    #Create a folder for the month
    savedir += nowtime.strftime("/%m")
    if not os.path.exists(os.path.join(savedir)):
        os.mkdir(os.path.join(savedir))
        print("MAKE_DIR: " + savedir)

    #Generate a folder for days
    savedir += nowtime.strftime("/%d")
    if not os.path.exists(os.path.join(savedir)):
        os.mkdir(os.path.join(savedir))
        print("MAKE_DIR: " + savedir)

    #time_Minutes_Generate a second folder

    savefile = savedir

    saveFileName = datetime.now().strftime("%H_%M_%S.png ")
    saveFileName = os.path.join(savedir, saveFileName)
    cv2.imwrite(saveFileName, img)
    print(str(savedir) +"Saved in")
  

Recommended Posts

Function to save images by date [python3]
[Keras] Personal memo to classify images by folder [Python]
[python] Convert date to string
How to save a table scraped by python to csv
Sort by date in python
Save images on the web to Drive with Python (Colab)
How to collect images in Python
Post images from Python to Tumblr
How to calculate date with python
How to use python zip function
python function ①
[Python] function
python function ②
Reintroduction to Python Decorators ~ Learn Decorators by Type ~
Answer to AtCoder Beginners Selection by Python3
Save video frame by frame with Python OpenCV
[Introduction to Udemy Python3 + Application] 48. Function definition
[python] How to use __command__, function explanation
[Introduction to Udemy Python3 + Application] 45. enumerate function
[Introduction to Udemy Python3 + Application] 41. Input function
[Introduction to Udemy Python3 + Application] 44. range function
[Introduction to Udemy Python3 + Application] 46. Zip function
Lambda function to take AMI backup (python)
Recommended books by 3 types related to Python
Convert Python date types to RFC822 format
Add Gaussian noise to images with python2.7
Python) Save scraping content to local PC
Upload images to Google Drive with Python
What I did to save Python memory
Get tweets with Google Cloud Function and automatically save images to Google Photos
[python] How to display list elements side by side
Add conversation function to slack bot (made by python) using Recruit's Talk API
Python date arithmetic
Automatically register function arguments to argparse in Python
Convert "number" of excel date to python datetime
To execute a Python enumerate function in JavaScript
python enumerate function
Convert PDFs to images in bulk with Python
How to plot multiple fits images side by side in galactic coordinates using python
Automatically save images and videos hit by Twitter search to iPhone using Pythonista3
Python> function> Closure
[Python] Generator function
Convert Windows epoch values to date with python
[Python] How to save images on the Web at once with Beautiful Soup
How to erase the characters output by Python
I made a module in C language to filter images loaded by Python
[Python] How to use hash function and tuple.
Python> function> Inner function
[Python] How to sort instances by instance variables
I want to sell Mercari by scraping python
Python function decorator
Execute Power Query by passing arguments to Python
To represent date, time, time, and seconds in Python
[Python] Continued-Convert PDF text to CSV page by page
"Backport" to python 2
Split Python images and arrange them side by side
[Python] You can save an object to a file by using the pickle module.
Add a function to tell the weather of today to slack bot (made by python)
Convert timezoned date and time to Unixtime in Python2.7
List of posts related to optimization by Python to docker
[Introduction to Python] How to iterate with the range function?