Create image_validator.py
Browse files
modules/ai_assistant/image_validator.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import logging
|
| 3 |
+
import base64
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
def validate_image(image_path, max_size_mb=10):
|
| 8 |
+
"""
|
| 9 |
+
التحقق من صحة الصورة وتحسينها قبل إرسالها إلى API
|
| 10 |
+
|
| 11 |
+
المعلمات:
|
| 12 |
+
image_path: مسار الصورة
|
| 13 |
+
max_size_mb: الحجم الأقصى المسموح به بالميجابايت
|
| 14 |
+
|
| 15 |
+
العوائد:
|
| 16 |
+
tuple: (صورة محسنة، نوع الملف، حالة الصلاحية، رسالة الخطأ)
|
| 17 |
+
"""
|
| 18 |
+
try:
|
| 19 |
+
# التحقق من وجود الملف
|
| 20 |
+
if not os.path.exists(image_path):
|
| 21 |
+
return None, None, False, f"الملف غير موجود: {image_path}"
|
| 22 |
+
|
| 23 |
+
# التحقق من حجم الملف
|
| 24 |
+
file_size_mb = os.path.getsize(image_path) / (1024 * 1024)
|
| 25 |
+
if file_size_mb > max_size_mb:
|
| 26 |
+
return None, None, False, f"حجم الملف كبير جداً: {file_size_mb:.2f} ميجابايت (الحد الأقصى: {max_size_mb} ميجابايت)"
|
| 27 |
+
|
| 28 |
+
# تحديد نوع الملف من امتداده
|
| 29 |
+
_, ext = os.path.splitext(image_path)
|
| 30 |
+
ext = ext.lower()
|
| 31 |
+
|
| 32 |
+
if ext in ('.jpg', '.jpeg'):
|
| 33 |
+
file_type = "image/jpeg"
|
| 34 |
+
elif ext == '.png':
|
| 35 |
+
file_type = "image/png"
|
| 36 |
+
elif ext == '.gif':
|
| 37 |
+
file_type = "image/gif"
|
| 38 |
+
elif ext == '.webp':
|
| 39 |
+
file_type = "image/webp"
|
| 40 |
+
else:
|
| 41 |
+
return None, None, False, f"نوع الملف غير مدعوم: {ext}"
|
| 42 |
+
|
| 43 |
+
# فتح الصورة باستخدام PIL للتحقق من صحتها
|
| 44 |
+
try:
|
| 45 |
+
with Image.open(image_path) as img:
|
| 46 |
+
# التحقق من أبعاد الصورة
|
| 47 |
+
width, height = img.size
|
| 48 |
+
if width > 4000 or height > 4000:
|
| 49 |
+
# تغيير حجم الصورة إذا كانت كبيرة جداً
|
| 50 |
+
img = img.resize((min(width, 4000), min(height, 4000)), Image.LANCZOS)
|
| 51 |
+
|
| 52 |
+
# تحويل الصورة إلى تنسيق مناسب
|
| 53 |
+
if ext == '.png' and img.mode == 'RGBA':
|
| 54 |
+
# تحويل الصور الشفافة إلى RGB
|
| 55 |
+
background = Image.new('RGB', img.size, (255, 255, 255))
|
| 56 |
+
background.paste(img, mask=img.split()[3])
|
| 57 |
+
img = background
|
| 58 |
+
|
| 59 |
+
# حفظ الصورة في ذاكرة مؤقتة
|
| 60 |
+
buffer = io.BytesIO()
|
| 61 |
+
img.save(buffer, format=file_type.split('/')[1].upper())
|
| 62 |
+
buffer.seek(0)
|
| 63 |
+
|
| 64 |
+
# تحويل إلى base64
|
| 65 |
+
file_content = buffer.getvalue()
|
| 66 |
+
file_base64 = base64.b64encode(file_content).decode('utf-8')
|
| 67 |
+
|
| 68 |
+
return file_base64, file_type, True, ""
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return None, None, False, f"خطأ في معالجة الصورة: {str(e)}"
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return None, None, False, f"خطأ في التحقق من الصورة: {str(e)}"
|