File size: 1,843 Bytes
20820c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import base64
from pathlib import Path
from PIL import Image
import io

def resize_image(image: Image.Image, max_size: int = 224) -> Image.Image:
    """
    Resize an image while maintaining aspect ratio.

    Args:
        image: PIL Image object to resize
        max_size: Maximum dimension (width or height) of the output image

    Returns:
        PIL Image: Resized image with maintained aspect ratio
    """
    # Get current dimensions
    width, height = image.size

    # Calculate scaling factor to fit within max_size
    scale = min(max_size / width, max_size / height)

    # Only resize if image is larger than max_size
    if scale < 1:
        new_width = int(width * scale)
        new_height = int(height * scale)
        image = image.resize(
            (new_width, new_height),
            Image.LANCZOS
        )

    return image


# Define your desired size
TARGET_SIZE = 16

# Define the image paths
image_paths = [
    "images/AAA0119DNBSPD01.jpg",
    "images/AAA0119DNBSPD02.jpg"
]

# Read and encode images
images = []
for path in image_paths:
    # Open the image
    img = Image.open(path)
    
    # Resize the image (using LANCZOS for high-quality downsampling)
    img = resize_image(img, max_size=TARGET_SIZE)
    
    # Convert to bytes
    buffered = io.BytesIO()
    img.save(buffered, format="JPEG")  # You can change format to PNG if needed
    
    # Encode to base64
    base64_image = base64.b64encode(buffered.getvalue()).decode('utf-8')
    images.append(base64_image)

# Make the request
print(images[0])
url = "http://localhost:8000/classify"
payload = {"images": [images[0]]}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)
print(f"Status Code: {response.status_code}")
print("Response Text:")
print(response.text)