File size: 4,379 Bytes
38cf962
 
 
 
da1975c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38cf962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da1975c
38cf962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from PIL import Image, ImageDraw, ImageFont
import os
import arabic_reshaper
from bidi.algorithm import get_display
import cv2
import numpy as np

def preprocess_image(image_path):
    """معالجة مسبقة للصورة لتحسين جودة OCR"""
    try:
        # قراءة الصورة
        image = cv2.imread(image_path)
        if image is None:
            return image_path
            
        # تحويل إلى تدرج الرمادي
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        
        # زيادة التباين
        enhanced = cv2.convertScaleAbs(gray, alpha=1.5, beta=30)
        
        # إزالة الضوضاء
        denoised = cv2.medianBlur(enhanced, 3)
        
        # حفظ الصورة المحسنة
        enhanced_path = "enhanced_image.png"
        cv2.imwrite(enhanced_path, denoised)
        
        return enhanced_path
        
    except Exception as e:
        print(f"❌ خطأ في معالجة الصورة: {e}")
        return image_path

def get_font(config):
    """الحصول على خط مناسب للغة العربية"""
    font_path = config.get("font_path", "fonts/arabic-font.ttf")
    font_size = config.get("font_size", 20)
    
    try:
        # محاولة تحميل الخط المحدد
        return ImageFont.truetype(font_path, font_size)
    except:
        try:
            # محاولة استخدام خط افتراضي للعربية
            return ImageFont.truetype("arial.ttf", font_size)
        except:
            try:
                # استخدام خط النظام إذا توفر
                return ImageFont.load_default()
            except:
                # الخط الافتراضي إذا فشل كل شيء
                return None

def render_text(image_path, boxes, texts, save_path="output.png", config=None):
    """إعادة رسم النصوص المترجمة على الصورة"""
    if config is None:
        config = {}
    
    try:
        # فتح الصورة
        im = Image.open(image_path).convert("RGBA")
        draw = ImageDraw.Draw(im)
        
        # الحصول على الخط
        font = get_font(config)
        
        # رسم كل نص في المربع المناسب
        for box, text in zip(boxes, texts):
            if not text or text == "[نص غير معروف]":  # تخطي النصوص الفارغة
                continue
                
            # معالجة النص العربي للعرض الصحيح
            reshaped_text = arabic_reshaper.reshape(text)
            bidi_text = get_display(reshaped_text)
                
            # حساب مركز المربع
            x_coords = [point[0] for point in box]
            y_coords = [point[1] for point in box]
            x_center = sum(x_coords) / len(x_coords)
            y_center = sum(y_coords) / len(y_coords)
            
            # حساب حجم النص
            if font:
                bbox = draw.textbbox((0, 0), bidi_text, font=font)
                text_width = bbox[2] - bbox[0]
                text_height = bbox[3] - bbox[1]
            else:
                text_width = len(text) * 10  # تقدير تقريبي
                text_height = 20
            
            # حساب موضع النص (مركز المربع)
            x_pos = x_center - text_width / 2
            y_pos = y_center - text_height / 2
            
            # التأكد من أن النص داخل حدود الصورة
            x_pos = max(10, min(x_pos, im.width - text_width - 10))
            y_pos = max(10, min(y_pos, im.height - text_height - 10))
            
            # رسم خلفية semi-transparent للنص
            if font:
                bbox = draw.textbbox((x_pos, y_pos), bidi_text, font=font)
                draw.rectangle(bbox, fill=(255, 255, 255, 200))
            
            # رسم النص
            if font:
                draw.text((x_pos, y_pos), bidi_text, fill=(0, 0, 0), font=font)
            else:
                draw.text((x_pos, y_pos), bidi_text, fill=(0, 0, 0))
        
        # حفظ الصورة الناتجة
        im.save(save_path)
        print(f"✅ تم حفظ الصورة في: {save_path}")
        
    except Exception as e:
        print(f"❌ خطأ في إعادة الرسم: {e}")
        raise