0

I have a folder of 1000 photos that I'd like to make a timelapse of. The photos are shot every 60 seconds, and I'd like to make a 10 minute interval timelapse. So I need to delete every 2nd to 9th photo and have it loop. For example, given 1000 photos in a folder, I'd like the script to keep photo 1, 10, 20, 30 and so on and so forth. At the end of this script, the folder should only contain 100 photos.

The following code removes every "10th" photo, which doesn't do exactly as I want:

import os
dir_to_clean = '/Users/myname/Desktop/TBD'
l = os.listdir(dir_to_clean)

for n in l[::10]:
    os.unlink(dir_to_clean + '/' + n)

How do I modify this code so that it deletes every 2nd-9th photo? It should still be able to run if the folder does not have an even number of files (e.g. if it has 1005 files).

Shalitha Suranga
  • 1,138
  • 8
  • 24
D500
  • 442
  • 5
  • 17

2 Answers2

1

First of all, you should not depend on the underlying OS indexing to do the sorting for you - you should sort the image list yourself (hopefully their names are in a lexicographical order).

Second, once sorted, just enumerate your list and don't delete every 10th item, e.g.:

import os

dir_to_clean = '/Users/myname/Desktop/TBD'
images = sorted(os.listdir(dir_to_clean))

for i, image in enumerate(images):
    if i % 10 != 0:
        os.remove(os.path.join(dir_to_clean, image))

For an image list like: ["image000.jpg", "image001.jpg", "image002.jpg", "image003.jpg", ... "image035.jpg"] this will delete all images except image000.jpg, image010.jpg, image020.jpg and image030.jpg.

zwer
  • 24,943
  • 3
  • 48
  • 66
  • Should I be saving the files as image000 -- image010.jpg etc? To me, this code looks for files that when divided by 10 does no equal 0? Thus, I should be naming the files with numbers? Or does it just simply count the order of the image-- so the tenth file would not be deleted..? – D500 Dec 11 '17 at 21:36
  • @D500 - no need to rename the files. They will be sorted in a lexicographical order and all files but every tenth one will be deleted no matter of their name. You can remove `sorted()` around the `os.listdir()` call and get sorting in order of OS indexing no matter of their name, but as I've warned this does not necessarily guarantee the order of creation. – zwer Dec 11 '17 at 22:07
  • Hrm.. Weird behaviours with the code. I ran it and it did successfully remove the 2-9th photo but then gave me an error and the rest of the images are corrupt. How do I fix this error? File "TimeLapseScript.py", line 8, in os.remove(os.path.join(dir_to_clean, image)) FileNotFoundError: [Errno 2] No such file or directory: '/Volumes/Seagate35TB/TimeLapse/Photos/Reduce/G0036768.JPG' – D500 Dec 12 '17 at 07:10
  • @D500 - Are you sure you didn't run the script twice in a row and that you don't have something else that might be altering the files in the target directory while you're running the script? – zwer Dec 12 '17 at 09:38
0

You wanted to keep all first and last in 10 minute range so I would go for something like this:

import os
dir_to_clean = '/Users/myname/Desktop/TBD'
images = sorted(os.listdir(dir_to_clean))

for i, x in enumerate(images):
    #print i, i % 10
    if i % 10 == 0 or i % 10 == 9:
        print("Keeping : " + str(x))
    else:
        print("Deleting: " + str(x))
        os.remove(os.path.join(dir_to_clean, image))
Ivonet
  • 2,492
  • 2
  • 15
  • 28