# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# the libraries you will need to run the program
import pandas as pd
import requests # to get image from the web
import shutil # to save it locally


df = pd.read_csv("~/Desktop/body_images.csv")

# Most of what follows is from: https://towardsdatascience.com/how-to-download-an-image-using-python-38a75cfa21c

for index, row in df.iterrows():
    image_url = row['URL']
    filename = image_url.split("/")[-3]
    r = requests.get(image_url, stream = True)
    ext = r.headers['content-type'].split('/')[-1] # converts response headers mime type to an extension (may not work with everything)
    filename += '.'
    filename += ext
    # Check if the image was retrieved successfully
    if r.status_code == 200:
        # Set decode_content value to True, otherwise the downloaded image file's size will be zero.
        r.raw.decode_content = True
        
        # Open a local file with wb ( write binary ) permission.
        with open(filename,'wb') as f:
            shutil.copyfileobj(r.raw, f)
        
        print('Image sucessfully Downloaded: ',filename)
    else:
        print('Image Couldn\'t be retreived')




StarcoderdataJupyterScriptsDedupFiltered