import pandas as pd import requests from PIL import Image from io import BytesIO import os import logging from urllib.parse import urlparse # Logging ayarları logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler("image_conversion.log"), logging.StreamHandler() ]) # Excel dosyasını oku df = pd.read_excel('guncel-pro-datalari.xlsx') # Kaydetmek istediğiniz klasörü oluşturun download_folder = 'downloaded_images' webp_folder = 'webp_images' os.makedirs(download_folder, exist_ok=True) os.makedirs(webp_folder, exist_ok=True) # Images kolonundaki resimleri indir ve webp'ye çevir for index, row in df.iterrows(): # Eğer hücre boş ise atla if pd.isna(row['Images']): logging.warning(f"Row {index} has empty Images column, skipping...") continue image_urls = row['Images'].split(',') webp_image_paths = [] for image_url in image_urls: try: # URL'deki sorgu parametrelerini kaldır parsed_url = urlparse(image_url.strip()) image_name = os.path.basename(parsed_url.path) image_path = os.path.join(download_folder, image_name) response = requests.get(image_url.strip()) if response.status_code == 200: # Resmi indir with open(image_path, 'wb') as file: file.write(response.content) # Resmi WebP formatına dönüştür img = Image.open(image_path) webp_name = image_name.rsplit('.', 1)[0] + '.webp' webp_path = os.path.join(webp_folder, webp_name) img.save(webp_path, 'webp') webp_image_paths.append(webp_path) logging.info(f"Processed image {image_url}") else: logging.error(f"Failed to download image {image_url}, status code: {response.status_code}") except Exception as e: logging.error(f"Error processing {image_url}: {e}") # WebP dosya yollarını virgülle ayırarak ekleyin df.at[index, 'Images'] = ', '.join(webp_image_paths) # Sonucu yeni bir Excel dosyasına yaz df.to_excel('converted_images.xlsx', index=False) logging.info("Dönüştürme işlemi tamamlandı.")