Last active
October 22, 2024 18:06
-
-
Save saamerm/9a8e362ca6a737d0bc400f5e569cd122 to your computer and use it in GitHub Desktop.
Python code to download images from the internet using URLs in a CSV/Google Sheet and resize them selectively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a table with 7 rows in Google Sheets and Copy the data with the headers and paste it in here: https://codepen.io/saamerm/pen/NWVZyEm | |
# Then get the json from that codepen and replace the variable in line 8 with the new json | |
import os | |
import requests | |
import json | |
# Sample JSON data | |
hotels = [ | |
{ | |
"HotelId": "1085", | |
"HotelName": "Sonesta Simply Suites Birmingham Hoover", | |
"HERO": "https://cdn.intelligencebank.com/us/share/a7ww/DnVVy/2ygDN/original/SIM_BirminghamHoover_4118882884_Exterior_Front_Night_R", | |
"M1": "https://cdn.intelligencebank.com/us/share/a7ww/6ygW1/8JBP6/original/SonestaSimplySuites_Interior_Amenity_LaundryFacility", | |
"M2": "https://cdn.intelligencebank.com/us/share/a7ww/6ygW1/l8kNR/original/Studio+Suite+Kitchenette", | |
"M3": "https://cdn.intelligencebank.com/us/share/a7ww/6ygW1/d2kXl/original/SonestaSimplySuites_Interior_Amenity_FitnessCenter_closeup", | |
"M4": "https://cdn.intelligencebank.com/us/share/a7ww/JV2OR/7B3rB/original/SonestaSimplySuites_GuestRoom_PetFriendly_6" | |
}, | |
{ | |
"HotelId": "1086", | |
"HotelName": "Sonesta Simply Suites Huntsville Research Park", | |
"HERO": "https://cdn.intelligencebank.com/us/share/a7ww/Pk8oE/D0yA9/original/SIM_HuntsvilleResearchPark_GuestRoom_OneBedroomSuite_FullRoom_1", | |
"M1": "https://cdn.intelligencebank.com/us/share/a7ww/Pk8oE/vPeyq/original/SIM_HuntsvilleResearchPark_Amenity_Laundry_1", | |
"M2": "https://cdn.intelligencebank.com/us/share/a7ww/Pk8oE/VPrV1/original/SIM_HuntsvilleResearchPark_GuestRoom_StudioSuite_Kitchen", | |
"M3": "https://cdn.intelligencebank.com/us/share/a7ww/Pk8oE/3My4v/original/SIM_HuntsvilleResearchPark_Amenity_TheLift_2", | |
"M4": "https://cdn.intelligencebank.com/us/share/a7ww/JV2OR/7B3rB/original/SonestaSimplySuites_GuestRoom_PetFriendly_6" | |
} | |
] | |
# Function to download an image from a URL and save it | |
def download_image(url, filename): | |
try: | |
response = requests.get(url) | |
if response.status_code == 200: | |
with open(filename, 'wb') as f: | |
f.write(response.content) | |
print(f"Downloaded: {filename}") | |
else: | |
return f"Failed to download {filename}. HTTP Status Code: {response.status_code}" | |
except Exception as e: | |
return f"Error downloading {filename}: {str(e)}" | |
return None | |
# Create a folder to store the downloaded images (if it doesn't exist) | |
save_folder = "downloaded_images" | |
os.makedirs(save_folder, exist_ok=True) | |
# List to track errors | |
error_log = [] | |
# Iterate through each hotel object | |
for hotel in hotels: | |
hotel_id = hotel["HotelId"] | |
# Download and save each image with the correct name | |
for image_key in ["HERO", "M1", "M2", "M3", "M4"]: | |
image_url = hotel.get(image_key) | |
if image_url: | |
# Generate the filename based on the HotelId and image key | |
filename = f"{hotel_id}-{image_key}.jpg" | |
filepath = os.path.join(save_folder, filename) | |
# Download and save the image, track errors if any | |
error = download_image(image_url, filepath) | |
if error: | |
error_log.append(error) | |
# Print the error report at the end of the process | |
if error_log: | |
print("\nDownload completed with the following errors:") | |
for error in error_log: | |
print(error) | |
else: | |
print("\nAll images have been downloaded successfully.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment