0

I have a folder ("pictures") with many jpg.files with the filename as timestamp (e.g. "09_02_01.jp" or "10_04_01.jpg"). Now I want to iterate through the folder and all jpg files and use the filenames as timestamps. With these timestamps I want to check if some filenames (jpgs) are nearby. For e.g. if a timestamp of a picture is 09_02_01.jpg and another is 10_04_01.jpg I want to get the difference of the timestamps of the pictures and if the pictures difference is less than 30 seconds e.g. than I want to delete one of the pictures, because I only want one picture for every 30 seconds.

At the moment I only got the jpg filename convertet into a datetime format.

from datetime import datetime
import os

def gettime(folder_dir):
    timediff_sec = 30

    for images in os.listdir(folder_dir):
        if images.endswith(".jpg"):

            fn, fext = os.path.splitext(images)

            # string name
            timestamp1 = fn

            # Convert String to datetime Object
            t1 = datetime.strptime(timestamp1, "%H_%M_%S").time()

            print(t1)

gettime("Bilder")
SumSum
  • 47
  • 10

1 Answers1

1

You can try my code below:

from datetime import datetime
import os

def gettime(folder_dir):
    timediff_sec = 30
    time_stamps = []
    for images in os.listdir(folder_dir):
        if images.endswith(".jpg"):
            fn, fext = os.path.splitext(images)

            # string name
            timestamp1 = fn

            # Convert String to datetime Object
            t1 = datetime.strptime(timestamp1, "%H_%M_%S")
            t1 = datetime.timestamp(t1)
            if len(time_stamps) == 0 or abs(int(t1) - min(time_stamps)) >= 30 or abs(int(t1) - max(time_stamps)) >= 30:
                time_stamps.append(int(t1))
            else:
                os.remove(os.path.join(folder_dir, images))

gettime("Bilder")
Viettel Solutions
  • 1,519
  • 11
  • 22
  • Hey, thanks for your try but unfortunately I get an Error when I run the code: Traceback (most recent call last): File "N:/BIK (übung)/Timestamp/main.py", line 21, in gettime("Bilder") File "N:/BIK (übung)/Timestamp/main.py", line 16, in gettime if len(time_stamps) == 0 or abs(t1 - min(time_stamps)) < 30 or abs(t1 - max(time_stamps)) < 30: TypeError: '<' not supported between instances of 'datetime.timedelta' and 'int' – SumSum May 11 '22 at 08:14
  • Now I got an other TypeError:Traceback (most recent call last): File "N:/BIK (übung)/Timestamp/main.py", line 21, in gettime("Bilder") File "N:/BIK (übung)/Timestamp/main.py", line 17, in gettime time_stamps.append(int(t1)) TypeError: int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime' – SumSum May 11 '22 at 08:43
  • @FabianBee i just edited. It worked! Can you try it again? – Viettel Solutions May 11 '22 at 09:01
  • Thank you very much, If i use the code in google colab in works perfectly, but do you know why in Pycharm I get: Traceback (most recent call last): File "N:/BIK (übung)/Timestamp/main.py", line 21, in gettime("Bilder") File "N:/BIK (übung)/Timestamp/main.py", line 15, in gettime t1 = datetime.timestamp(t1) OSError: [Errno 22] Invalid argument – SumSum May 11 '22 at 09:14
  • Can you capture your code in Pycharm and send me a link? Is "Bilder" directory exist in your local machine? – Viettel Solutions May 11 '22 at 10:07
  • https://stackoverflow.com/questions/72198717/oserror-errno-22-invalid-argument-in-pycharm – SumSum May 11 '22 at 10:21
  • @SumSum a have already reply that question. Can you try it? If it work, please accept 2 answer for me! Many thanks! – Viettel Solutions May 12 '22 at 02:20
  • Yes I accepted the answer, I know I am a bit annoying but can you pls tell me how datetime.strptime is converting to datetime.timestamp(t1)? I mean how does I get from the image file with the name 09_02_01 the value -2208956275 ? – SumSum May 12 '22 at 05:20
  • 1
    @SumSum i just convert to timestamp to units of seconds. Then i use this value to calculate the gaps of times. I hope you understand. – Viettel Solutions May 12 '22 at 06:52