-
Notifications
You must be signed in to change notification settings - Fork 0
/
aritzia-fashion-image-dowloader.py
58 lines (45 loc) · 1.72 KB
/
aritzia-fashion-image-dowloader.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import time
import csv
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from PIL import Image
import io
import os
import re
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
input_csv = "aritzia_product_data.csv"
output_folder = "aritzia_images"
os.makedirs(output_folder, exist_ok=True)
def sanitize_filename(filename):
filename = re.sub(r'[^\w\s-]', '_', filename)
filename = re.sub(r'[/\\]', '_', filename)
return filename
with open(input_csv, mode='r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
image_url = row["Image_URL"]
name = row["Name"]
color = row["Color"]
try:
driver.get(image_url)
time.sleep(2)
image_element = driver.find_element(By.TAG_NAME, 'img')
location = image_element.location
size = image_element.size
screenshot = driver.get_screenshot_as_png()
image = Image.open(io.BytesIO(screenshot))
left = location['x']
top = location['y']
right = left + size['width']
bottom = top + size['height']
cropped_image = image.crop((left, top, right, bottom))
safe_name = sanitize_filename(name)
safe_color = sanitize_filename(color)
output_path = f"{output_folder}/{safe_name}_{safe_color}.jpg"
cropped_image.save(output_path)
print(f"Screenshot saved for {name} - {color}")
except Exception as e:
print(f"Error capturing screenshot for {name}: {e}")
driver.quit()