Spaces:
Sleeping
Sleeping
import streamlit as st | |
from PIL import Image | |
import numpy as np | |
import os | |
import requests | |
from io import BytesIO | |
def enhance_multiple_images(uploaded_images, enhancement_type): | |
""" | |
تحسين مجموعة من الصور المرفوعة | |
""" | |
enhanced_images = [] | |
descriptions = [] | |
for uploaded_image in uploaded_images: | |
try: | |
# فتح الصورة المرفوعة | |
image = Image.open(uploaded_image) | |
# تحويل الصورة إلى مصفوفة NumPy للمعالجة | |
img_array = np.array(image) | |
# تطبيق التحسينات حسب النوع المطلوب | |
if enhancement_type == "brightness": | |
# زيادة السطوع | |
enhanced_array = np.clip(img_array * 1.2, 0, 255).astype(np.uint8) | |
description = "تم زيادة سطوع الصورة لإبراز تفاصيل الشاليه بشكل أفضل" | |
elif enhancement_type == "contrast": | |
# زيادة التباين | |
mean = np.mean(img_array, axis=(0, 1), keepdims=True) | |
enhanced_array = np.clip((img_array - mean) * 1.3 + mean, 0, 255).astype(np.uint8) | |
description = "تم تحسين تباين الصورة لإبراز الألوان والتفاصيل" | |
elif enhancement_type == "sharpness": | |
# محاكاة زيادة الحدة | |
# في التطبيق الفعلي يمكن استخدام مرشحات أكثر تعقيداً | |
enhanced_array = img_array.copy() | |
description = "تم زيادة حدة الصورة لإظهار التفاصيل الدقيقة" | |
elif enhancement_type == "professional": | |
# تحسين احترافي (مزيج من التحسينات) | |
# زيادة التباين قليلاً | |
mean = np.mean(img_array, axis=(0, 1), keepdims=True) | |
enhanced_array = np.clip((img_array - mean) * 1.15 + mean, 0, 255).astype(np.uint8) | |
description = "تم تطبيق تحسين احترافي شامل على الصورة لجعلها أكثر جاذبية" | |
else: | |
# إذا لم يتم تحديد نوع التحسين، إرجاع الصورة كما هي | |
enhanced_array = img_array | |
description = "لم يتم تطبيق أي تحسينات على الصورة" | |
# تحويل المصفوفة المحسنة إلى صورة | |
enhanced_image = Image.fromarray(enhanced_array) | |
enhanced_images.append(enhanced_image) | |
descriptions.append(description) | |
except Exception as e: | |
print(f"خطأ في تحسين الصورة: {str(e)}") | |
enhanced_images.append(image) # إرجاع الصورة الأصلية في حالة الخطأ | |
descriptions.append(f"حدث خطأ أثناء التحسين: {str(e)}") | |
return enhanced_images, descriptions | |
def create_image_prompt(chalet_data): | |
""" | |
إنشاء وصف نصي للصورة بناءً على بيانات الشاليه | |
""" | |
prompt = f"A beautiful chalet in {chalet_data['location']}, " | |
if 'style' in chalet_data and chalet_data['style']: | |
prompt += f"{chalet_data['style']} style, " | |
if 'features' in chalet_data and chalet_data['features']: | |
prompt += f"featuring {', '.join(chalet_data['features'])}, " | |
prompt += "photorealistic, high quality, 4K" | |
return prompt | |
def suggest_image_improvements(chalet_data): | |
""" | |
اقتراح تحسينات للصورة بناءً على بيانات الشاليه | |
""" | |
suggestions = [] | |
# اقتراحات عامة | |
suggestions.append("التقاط الصورة في وقت الغروب أو الشروق لإضافة ألوان دافئة") | |
suggestions.append("التأكد من نظافة وترتيب المكان قبل التصوير") | |
suggestions.append("استخدام إضاءة طبيعية كلما أمكن") | |
# اقتراحات خاصة بنوع الشاليه | |
if 'target_audience' in chalet_data: | |
if chalet_data['target_audience'] == 'عائلات': | |
suggestions.append("إظهار المساحات العائلية ومناطق اللعب في الصورة") | |
suggestions.append("التقاط صور للمسبح إذا كان متوفراً") | |
elif chalet_data['target_audience'] == 'أزواج': | |
suggestions.append("التركيز على الخصوصية والمناظر الرومانسية") | |
suggestions.append("إظهار مناطق الاسترخاء والتراسات الخاصة") | |
elif chalet_data['target_audience'] == 'شباب': | |
suggestions.append("إظهار مناطق النشاطات والترفيه") | |
suggestions.append("التقاط صور للشاطئ أو المناطق القريبة") | |
# اقتراحات بناءً على الموقع | |
if 'location' in chalet_data: | |
if 'السخنة' in chalet_data['location']: | |
suggestions.append("إبراز إطلالة البحر في الصور") | |
suggestions.append("التقاط صور للشاطئ القريب") | |
elif 'الساحل الشمالي' in chalet_data['location']: | |
suggestions.append("إظهار المياه الفيروزية والشواطئ البيضاء") | |
elif 'شرم الشيخ' in chalet_data['location']: | |
suggestions.append("إظهار الشعاب المرجانية القريبة إذا كانت متوفرة") | |
return suggestions | |
def save_enhanced_image(image, filename): | |
""" | |
حفظ الصورة المحسنة | |
""" | |
try: | |
# إنشاء مجلد للصور المحسنة إذا لم يكن موجوداً | |
if not os.path.exists("enhanced_images"): | |
os.makedirs("enhanced_images") | |
# حفظ الصورة | |
save_path = os.path.join("enhanced_images", filename) | |
image.save(save_path) | |
return save_path | |
except Exception as e: | |
print(f"خطأ في حفظ الصورة: {str(e)}") | |
return None | |
def download_image_from_url(image_url): | |
""" | |
تنزيل صورة من رابط URL | |
""" | |
try: | |
response = requests.get(image_url) | |
image = Image.open(BytesIO(response.content)) | |
return image | |
except Exception as e: | |
print(f"خطأ في تنزيل الصورة: {str(e)}") | |
return None | |
def get_chalet_images_from_dataset(dataset, chalet_name): | |
""" | |
الحصول على صور الشاليه من مجموعة البيانات | |
""" | |
try: | |
df = dataset.to_pandas() | |
# البحث عن الشاليه بالاسم | |
chalet_data = df[df['name'] == chalet_name] | |
if chalet_data.empty or 'image_url' not in chalet_data.columns: | |
return [] | |
# الحصول على روابط الصور | |
image_urls = chalet_data['image_url'].tolist() | |
# تنزيل الصور | |
images = [] | |
for url in image_urls: | |
if url and isinstance(url, str): | |
image = download_image_from_url(url) | |
if image: | |
images.append(image) | |
return images | |
except Exception as e: | |
print(f"خطأ في الحصول على صور الشاليه: {str(e)}") | |
return [] | |