rrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrr

Reputation: 23

How to copy alternate images from one folder to another in python

I want to implement a python code which would select a 1 image after 3 images and so on till the last image in an sequential manner in the specified folder and copy those images to another folder.

Example : As shown in the screenshot

link : https://i.sstatic.net/DPdOd.png

Upvotes: 2

Views: 2421

Answers (3)

Ghantey
Ghantey

Reputation: 636

The solution is same but I think it is more clear for all

import os
import shutil

path_to_your_files = 'your pics path'
copy_to_path = 'destination for your copy'

files_list = sorted(os.listdir(path_to_your_files))
orders = range(1, len(files_list) , 4)

for order in orders:
    files = files_list[order] # getting 1 image after 3 images
    shutil.copyfile(os.path.join(path_to_your_files, files), os.path.join(copy_to_path, files))  # copying images to destination folder

Upvotes: 3

anand_v.singh
anand_v.singh

Reputation: 2838

import os
from shutil import copyfile
files = sorted(os.listdir('Source Folder'))
4thFile = [fileName for index, file in zip(range(len(files)),files) if not index%4]
for file in 4thFile:
    copyfile(os.path.join(src_path, f), os.path.join(dest_path, file))

That should get the job done.

Upvotes: 2

Alex Yu
Alex Yu

Reputation: 3537

You can:

import os
files = os.listdir('YOUR PICS DIRECTORY HERE')
every_4th_files=[f for idx,f in  zip(range(len(files)), files) if not idx%4]

Is it what you need?

Edit

To copy images I recommend to use shutil.copyfile.

If you encounter a problem - inform about it.

Upvotes: 2

Related Questions