File size: 2,666 Bytes
88f15c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
bucket_options = {
    640: [
        (416, 960),
        (448, 864),
        (480, 832),
        (512, 768),
        (544, 704),
        (576, 672),
        (608, 640),
        (640, 608),
        (672, 576),
        (704, 544),
        (768, 512),
        (832, 480),
        (864, 448),
        (960, 416),
    ],
    # Add options for other resolutions with similar aspect ratios
    128: [
        (96, 160),
        (112, 144),
        (128, 128),
        (144, 112),
        (160, 96),
    ],
    256: [
        (192, 320),
        (224, 288),
        (256, 256),
        (288, 224),
        (320, 192),
    ],
    384: [
        (256, 512),
        (320, 448),
        (384, 384),
        (448, 320),
        (512, 256),
    ],
    512: [
        (352, 704),
        (384, 640),
        (448, 576),
        (512, 512),
        (576, 448),
        (640, 384),
        (704, 352),
    ],
    768: [
        (512, 1024),
        (576, 896),
        (640, 832),
        (704, 768),
        (768, 704),
        (832, 640),
        (896, 576),
        (1024, 512),
    ],
}


def find_nearest_bucket(h, w, resolution=640):
    # Use the provided resolution or find the closest available bucket size
    print(f"find_nearest_bucket called with h={h}, w={w}, resolution={resolution}")
    
    if resolution not in bucket_options:
        # Find the closest available resolution
        available_resolutions = list(bucket_options.keys())
        closest_resolution = min(available_resolutions, key=lambda x: abs(x - resolution))
        print(f"Resolution {resolution} not found in bucket options, using closest available: {closest_resolution}")
        resolution = closest_resolution
    else:
        print(f"Resolution {resolution} found in bucket options")
    
    # Calculate the aspect ratio of the input image
    input_aspect_ratio = w / h if h > 0 else 1.0
    print(f"Input aspect ratio: {input_aspect_ratio:.4f}")
    
    min_diff = float('inf')
    best_bucket = None
    
    # Find the bucket size with the closest aspect ratio to the input image
    for (bucket_h, bucket_w) in bucket_options[resolution]:
        bucket_aspect_ratio = bucket_w / bucket_h if bucket_h > 0 else 1.0
        # Calculate the difference in aspect ratios
        diff = abs(bucket_aspect_ratio - input_aspect_ratio)
        if diff < min_diff:
            min_diff = diff
            best_bucket = (bucket_h, bucket_w)
        print(f"  Checking bucket ({bucket_h}, {bucket_w}), aspect ratio={bucket_aspect_ratio:.4f}, diff={diff:.4f}, current best={best_bucket}")
    
    print(f"Using resolution {resolution}, selected bucket: {best_bucket}")
    return best_bucket