dd33301230 commited on
Commit
e26b83e
·
verified ·
1 Parent(s): bae8ca9

Upload bucket_tools.py

Browse files
Files changed (1) hide show
  1. bucket_tools.py +92 -0
bucket_tools.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 安全な解像度値のリスト
2
+ SAFE_RESOLUTIONS = [512, 640, 768, 960, 1080]
3
+
4
+ # 標準解像度のバケット設定
5
+ bucket_options = {
6
+ 512: [
7
+ (352, 704),
8
+ (384, 640),
9
+ (416, 608),
10
+ (448, 576),
11
+ (480, 544),
12
+ (512, 512),
13
+ (544, 480),
14
+ (576, 448),
15
+ (608, 416),
16
+ (640, 384),
17
+ (704, 352),
18
+ ],
19
+ 640: [
20
+ (416, 960),
21
+ (448, 864),
22
+ (480, 832),
23
+ (512, 768),
24
+ (544, 704),
25
+ (576, 672),
26
+ (608, 640),
27
+ (640, 608),
28
+ (672, 576),
29
+ (704, 544),
30
+ (768, 512),
31
+ (832, 480),
32
+ (864, 448),
33
+ (960, 416),
34
+ ],
35
+ 768: [
36
+ (512, 1024),
37
+ (576, 960),
38
+ (640, 896),
39
+ (704, 832),
40
+ (768, 768),
41
+ (832, 704),
42
+ (896, 640),
43
+ (960, 576),
44
+ (1024, 512),
45
+ ],
46
+ 960: [
47
+ (640, 1280),
48
+ (704, 1152),
49
+ (768, 1024),
50
+ (832, 960),
51
+ (896, 896),
52
+ (960, 832),
53
+ (1024, 768),
54
+ (1152, 704),
55
+ (1280, 640),
56
+ ],
57
+ 1080: [
58
+ (720, 1440),
59
+ (768, 1344),
60
+ (832, 1248),
61
+ (896, 1152),
62
+ (960, 1080),
63
+ (1024, 1024),
64
+ (1080, 960),
65
+ (1152, 896),
66
+ (1248, 832),
67
+ (1344, 768),
68
+ (1440, 720),
69
+ ],
70
+ }
71
+
72
+
73
+ def find_nearest_bucket(h, w, resolution=640):
74
+ """最も適切なアスペクト比のバケットを見つける関数"""
75
+ # 安全な解像度に丸める
76
+ if resolution not in SAFE_RESOLUTIONS:
77
+ # 最も近い安全な解像度を選択
78
+ closest_resolution = min(SAFE_RESOLUTIONS, key=lambda x: abs(x - resolution))
79
+ print(f"Warning: Resolution {resolution} is not in safe list. Using {closest_resolution} instead.")
80
+ resolution = closest_resolution
81
+
82
+ min_metric = float('inf')
83
+ best_bucket = None
84
+ for (bucket_h, bucket_w) in bucket_options[resolution]:
85
+ # アスペクト比の差を計算
86
+ metric = abs(h * bucket_w - w * bucket_h)
87
+ if metric <= min_metric:
88
+ min_metric = metric
89
+ best_bucket = (bucket_h, bucket_w)
90
+
91
+ return best_bucket
92
+