File size: 1,045 Bytes
3f8c153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import matplotlib.pyplot as plt
from typing import List, Dict
import io
from PIL import Image

def plot_results(results: List[Dict], query: str, num_images: int = 3) -> Image.Image:
    """
    Plot search results as image grid.
    
    Returns:
        PIL Image of the plot
    """
    fig, axes = plt.subplots(1, num_images, figsize=(20, 7))
    if num_images == 1:
        axes = [axes]
    
    fig.suptitle(f"🔍 Query: {query}", fontsize=18, fontweight='bold')
    
    for i, ax in enumerate(axes):
        if i < len(results):
            result = results[i]
            img = plt.imread(result["image_path"])
            ax.imshow(img)
            ax.set_title(
                f"Rank {result['rank']} | Score: {result['score']:.4f}",
                fontsize=14
            )
        ax.axis('off')
    
    plt.tight_layout()
    
    # Convert to PIL Image
    buf = io.BytesIO()
    plt.savefig(buf, format='png', bbox_inches='tight', dpi=100)
    buf.seek(0)
    img_pil = Image.open(buf)
    plt.close()
    
    return img_pil