-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_handler.py
More file actions
32 lines (26 loc) · 881 Bytes
/
image_handler.py
File metadata and controls
32 lines (26 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import os
import requests
from PIL import Image
from io import BytesIO
IMAGE_DIR = "images"
# Ensure the images directory exists
if not os.path.exists(IMAGE_DIR):
os.makedirs(IMAGE_DIR)
def save_image(image_url):
try:
response = requests.get(image_url)
response.raise_for_status()
image = Image.open(BytesIO(response.content))
image_name = os.path.basename(image_url)
image_path = os.path.join(IMAGE_DIR, image_name)
image.save(image_path)
print(f"Image saved to {image_path}")
return image_path
except Exception as e:
print(f"Failed to save image from {image_url}: {e}")
return None
def delete_images():
for file_name in os.listdir(IMAGE_DIR):
file_path = os.path.join(IMAGE_DIR, file_name)
if os.path.isfile(file_path):
os.remove(file_path)