|
| 1 | +import os |
| 2 | +import requests |
| 3 | +from PIL import Image |
| 4 | +#Import necessary libraries |
| 5 | + |
| 6 | +website_url = "https://example.com/images/" |
| 7 | +#Define the URL of the website containing the images you want to download |
| 8 | + |
| 9 | +def download_images(url, download_folder): |
| 10 | + response = requests.get(url) |
| 11 | + image_names = ["image1.jpg", "image2.jpg", "image3.jpg"] # Replace with actual image names or fetch them dynamically |
| 12 | + for name in image_names: |
| 13 | + image_url = url + name |
| 14 | + image_path = os.path.join(download_folder, name) |
| 15 | + with open(image_path, 'wb') as f: |
| 16 | + f.write(response.content) |
| 17 | +#Create a function to download images from the website |
| 18 | + |
| 19 | +def resize_images(input_folder, output_folder, target_size): |
| 20 | + for filename in os.listdir(input_folder): |
| 21 | + input_path = os.path.join(input_folder, filename) |
| 22 | + output_path = os.path.join(output_folder, filename) |
| 23 | + img = Image.open(input_path) |
| 24 | + img_resized = img.resize(target_size) |
| 25 | + img_resized.save(output_path) |
| 26 | + |
| 27 | +#Create a function to resize images |
| 28 | +def resize_images(input_folder, output_folder, target_size): |
| 29 | + for filename in os.listdir(input_folder): |
| 30 | + input_path = os.path.join(input_folder, filename) |
| 31 | + output_path = os.path.join(output_folder, filename) |
| 32 | + img = Image.open(input_path) |
| 33 | + img_resized = img.resize(target_size) |
| 34 | + img_resized.save(output_path) |
| 35 | + |
| 36 | + |
| 37 | +#Define the target size for the resized images |
| 38 | +target_size = (300, 200) # Width x Height in pixels |
| 39 | + |
| 40 | +def organize_images_by_dimensions(input_folder): |
| 41 | + for filename in os.listdir(input_folder): |
| 42 | + input_path = os.path.join(input_folder, filename) |
| 43 | + img = Image.open(input_path) |
| 44 | + width, height = img.size |
| 45 | + folder_name = f"{width}x{height}" |
| 46 | + folder_path = os.path.join(input_folder, folder_name) |
| 47 | + if not os.path.exists(folder_path): |
| 48 | + os.mkdir(folder_path) |
| 49 | + output_path = os.path.join(folder_path, filename) |
| 50 | + shutil.move(input_path, output_path |
| 51 | +#Create a function to organize images into folders based on their dimensions |
| 52 | + |
| 53 | +#Main function to execute the automation script |
| 54 | +if __name__ == "__main__": |
| 55 | + download_folder = "downloads" |
| 56 | + os.makedirs(download_folder, exist_ok=True) |
| 57 | + download_images(website_url, download_folder) |
| 58 | + |
| 59 | + resize_folder = "resized" |
| 60 | + os.makedirs(resize_folder, exist_ok=True) |
| 61 | + resize_images(download_folder, resize_folder, target_size) |
| 62 | + |
| 63 | + organize_images_by_dimensions(resize_folder) |
| 64 | + |
0 commit comments