Update modules/ai_assistant/ai_app.py
Browse files
modules/ai_assistant/ai_app.py
CHANGED
@@ -84,10 +84,30 @@ class ClaudeAIService:
|
|
84 |
# الحصول على مفتاح API
|
85 |
api_key = self.get_api_key()
|
86 |
|
87 |
-
# قراءة محتوى الصورة
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
# تحويل المحتوى إلى Base64
|
92 |
file_base64 = base64.b64encode(file_content).decode('utf-8')
|
93 |
|
|
|
84 |
# الحصول على مفتاح API
|
85 |
api_key = self.get_api_key()
|
86 |
|
87 |
+
# قراءة محتوى الصورة مع ضغطها إذا كان حجمها كبيرًا
|
88 |
+
file_size = os.path.getsize(image_path)
|
89 |
+
if file_size > 5 * 1024 * 1024: # إذا كان الحجم أكبر من 5 ميجابايت
|
90 |
+
# فتح الصورة وضغطها
|
91 |
+
with Image.open(image_path) as img:
|
92 |
+
# حفظ الصورة المضغوطة في ذاكرة مؤقتة
|
93 |
+
compressed_img_buffer = io.BytesIO()
|
94 |
+
|
95 |
+
# حساب معامل الجودة المناسب (كلما قل الرقم، قلت الجودة وحجم الملف)
|
96 |
+
quality = min(95, int((5 * 1024 * 1024 / file_size) * 100))
|
97 |
+
|
98 |
+
# حفظ الصورة بالجودة المحسوبة
|
99 |
+
img.save(compressed_img_buffer, format='JPEG', quality=quality)
|
100 |
+
compressed_img_buffer.seek(0)
|
101 |
+
|
102 |
+
|
103 |
+
# استخدام البيانات المضغوطة
|
104 |
+
file_content = compressed_img_buffer.read()
|
105 |
+
print(f"تم ضغط الصورة من {file_size/1024/1024:.2f} ميجابايت إلى {len(file_content)/1024/1024:.2f} ميجابايت")
|
106 |
+
else:
|
107 |
+
# استخدام الملف الأصلي إذا كان حجمه مقبول
|
108 |
+
with open(image_path, 'rb') as f:
|
109 |
+
file_content = f.read()
|
110 |
+
|
111 |
# تحويل المحتوى إلى Base64
|
112 |
file_base64 = base64.b64encode(file_content).decode('utf-8')
|
113 |
|