Thursday, December 10, 2020

Python - Separating photos into subfolders

Read Images in a Folder and Move to Sub folder Based on given Condition

Few weeks ago on this blog post, we downloaded pictures of all the US presidents. On this post, we want to separate the photos into subfolders depending on whether the president is from 'Democratic' or 'Republican'. 

If this sounds interesting to you, then keep reading!

Here we got photos/picture of US presidents in a folder and we want to split them into three folders depending on their political parties as either 'Democratic' or 'Republican' or Others.

Note that 'Grover Cleveland' was president on two different periods ('March 4, 1885 – March 4, 1889'and 'March 4, 1893 – March 4, 1897') - see yellow highlight below, however his picture was captured just once.

Also, some presidents had their party as some merge between the two parties or others such as "Democratic-Republican" or Republican (National Union) or National Union/Democratic. We are not interested in such parties, we are looking at just  'Democratic', 'Republican' and Others.

Upon executing the script below, it should collect the images from the folder 'US Presidents' and populate the other three folders depending on where the photo belongs as already explained above.

As you see, the folder "Democratic, Republican and Others" are now filled with photos.

import glob
import shutil
import pandas as pd

# Read in the US presidents spreadsheet...
us_presidents_df = pd.read_excel(r"C:\Users\Yusuf_08039508010\Desktop\US Presidents\USA List - Copy.xlsx")

# Contruct dictonary of from the columns name:party... we will use it later
usdict = dict(zip(us_presidents_df['Name'], us_presidents_df['Party']))

# Read the photos/pictures from the folder...
photos = glob.glob(r'US Presidents\*')

# Get names out of pics path...
presi_name_list = []
for p in photos:
    presi_name_list.append(p.split('\\')[-1].replace('.jpg', '')) # .split('.')[0]
    # alternatively we could use: os.listdir() to get image/photo file names

# define the destination folder locations...
democratic_folder = "Democratic"
republican_folder = "Republican"
others_folder = "Others"

# Loop through the list of names and photos, check if name matches the value of constructed dictionary (usdict)
for name, pic in zip(presi_name_list, photos):
    if usdict[name] == 'Democratic':
        print('This is a Democrat', name, pic)
        shutil.move(pic, democratic_folder) # (src, destination)
    elif usdict[name] == 'Republican':
        print('This is a Democrat Republican', name, pic)
        shutil.move(pic, republican_folder) # (src, destination)
        shutil.move(pic, others_folder) # (src, destination)

The script pretty straight forward. We only made use of three modules (glob, shutil and pandas). While glob and shutil are standard libraries (used for File and Directory Access) that come with python installation, you will have to install pandas which is a third party library.

That is it!

No comments:

Post a Comment