-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_tiler.py
More file actions
257 lines (208 loc) · 9.27 KB
/
grid_tiler.py
File metadata and controls
257 lines (208 loc) · 9.27 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python3
"""
Grid Tiler - 画像タイリングツール
1枚の画像をNxNグリッドに並べて1枚の画像として出力する。
"""
import sys
import cv2
import numpy as np
from pathlib import Path
from typing import Optional
from PySide6.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QLabel, QPushButton, QFileDialog, QMessageBox, QGroupBox, QComboBox
)
from PySide6.QtCore import Qt
# パスを追加
sys.path.insert(0, str(Path(__file__).parent))
from cv2_utils import load_image_as_bgra, save_image
class GridTilerWindow(QMainWindow):
"""グリッドタイリングメインウィンドウ"""
MAX_OUTPUT_SIZE = 2400
def __init__(self):
super().__init__()
self.setWindowTitle('Grid Tiler - 画像タイリングツール')
self.setMinimumSize(400, 300)
self.setAcceptDrops(True)
self.source_image: Optional[np.ndarray] = None
self.source_path: str = ''
self._setup_ui()
def _setup_ui(self):
central = QWidget()
self.setCentralWidget(central)
main_layout = QVBoxLayout(central)
# グリッドサイズ選択
grid_group = QGroupBox('グリッドサイズ')
grid_layout = QHBoxLayout(grid_group)
grid_layout.addWidget(QLabel('レイアウト:'))
self.combo_grid = QComboBox()
self.combo_grid.addItem('2x2(4枚)', 2)
self.combo_grid.addItem('3x3(9枚)', 3)
self.combo_grid.setCurrentIndex(0)
self.combo_grid.currentIndexChanged.connect(self._on_grid_changed)
grid_layout.addWidget(self.combo_grid)
grid_layout.addStretch()
main_layout.addWidget(grid_group)
# ドロップゾーン
drop_group = QGroupBox('画像入力')
drop_layout = QVBoxLayout(drop_group)
self.drop_zone = QLabel('画像をここにドロップ')
self.drop_zone.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.drop_zone.setStyleSheet(
"background-color: #1e1e1e; border: 2px dashed #3e3e42; "
"border-radius: 5px; padding: 30px; color: #888; font-size: 14px;"
)
self.drop_zone.setMinimumHeight(120)
drop_layout.addWidget(self.drop_zone)
self.btn_select = QPushButton('ファイルを選択...')
self.btn_select.clicked.connect(self._select_file)
drop_layout.addWidget(self.btn_select)
main_layout.addWidget(drop_group)
# 情報表示
info_group = QGroupBox('情報')
info_layout = QVBoxLayout(info_group)
self.lbl_filename = QLabel('ファイル: 未選択')
self.lbl_filename.setStyleSheet('color: #888;')
info_layout.addWidget(self.lbl_filename)
self.lbl_input_size = QLabel('入力サイズ: -')
self.lbl_input_size.setStyleSheet('color: #888;')
info_layout.addWidget(self.lbl_input_size)
self.lbl_output_size = QLabel('出力サイズ: -')
self.lbl_output_size.setStyleSheet('color: #888;')
info_layout.addWidget(self.lbl_output_size)
main_layout.addWidget(info_group)
# 保存ボタン
save_group = QGroupBox('出力')
save_layout = QVBoxLayout(save_group)
self.btn_save = QPushButton('タイリング画像を保存...')
self.btn_save.setStyleSheet(
'background-color: #16a34a; color: white; font-weight: bold; padding: 10px;'
)
self.btn_save.clicked.connect(self._save_image)
self.btn_save.setEnabled(False)
save_layout.addWidget(self.btn_save)
main_layout.addWidget(save_group)
main_layout.addStretch()
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
def dropEvent(self, event):
urls = event.mimeData().urls()
if urls:
path = urls[0].toLocalFile()
self._load_image(path)
def _select_file(self):
path, _ = QFileDialog.getOpenFileName(
self, '画像を選択', '',
'画像 (*.png *.jpg *.jpeg *.bmp *.webp)'
)
if path:
self._load_image(path)
def _load_image(self, path: str):
try:
image = load_image_as_bgra(path)
except Exception as e:
QMessageBox.warning(self, 'エラー', f'画像の読み込みに失敗しました: {e}')
return
# Noneチェック(OpenCVは読み込み失敗時にNoneを返す場合がある)
if image is None:
QMessageBox.warning(self, 'エラー', '画像の読み込みに失敗しました。ファイルが破損しているか、対応していない形式です。')
return
self.source_image = image
self.source_path = path
h, w = image.shape[:2]
filename = Path(path).name
self.lbl_filename.setText(f'ファイル: {filename}')
self.lbl_filename.setStyleSheet('color: #4ade80;')
self.lbl_input_size.setText(f'入力サイズ: {w} x {h}')
self.lbl_input_size.setStyleSheet('color: #ccc;')
self._update_output_info()
self.btn_save.setEnabled(True)
def _on_grid_changed(self):
if self.source_image is not None:
self._update_output_info()
def _update_output_info(self):
if self.source_image is None:
return
grid_size = self.combo_grid.currentData()
h, w = self.source_image.shape[:2]
# タイリング後のサイズ計算
tiled_h = h * grid_size
tiled_w = w * grid_size
# 2400px制限適用後のサイズ
scale = min(self.MAX_OUTPUT_SIZE / tiled_h, self.MAX_OUTPUT_SIZE / tiled_w, 1.0)
final_h = max(1, int(tiled_h * scale))
final_w = max(1, int(tiled_w * scale))
if scale < 1.0:
self.lbl_output_size.setText(
f'出力サイズ: {final_w} x {final_h}({tiled_w}x{tiled_h}から縮小)'
)
else:
self.lbl_output_size.setText(f'出力サイズ: {final_w} x {final_h}')
self.lbl_output_size.setStyleSheet('color: #60a5fa;')
def _create_tiled_image(self) -> np.ndarray:
"""タイリング画像を生成(OOM対策: 先に縮小してからタイリング)"""
grid_size = self.combo_grid.currentData()
h, w = self.source_image.shape[:2]
# タイリング後のサイズ計算
tiled_h = h * grid_size
tiled_w = w * grid_size
# 2400px制限のスケール計算
scale = min(self.MAX_OUTPUT_SIZE / tiled_h, self.MAX_OUTPUT_SIZE / tiled_w, 1.0)
# OOM対策: 縮小が必要な場合は先に元画像を縮小してからタイリング
if scale < 1.0:
# 各セルの目標サイズを計算
cell_h = max(1, int(h * scale))
cell_w = max(1, int(w * scale))
resized = cv2.resize(self.source_image, (cell_w, cell_h), interpolation=cv2.INTER_AREA)
tiled = np.tile(resized, (grid_size, grid_size, 1))
else:
# 縮小不要: そのままタイリング
tiled = np.tile(self.source_image, (grid_size, grid_size, 1))
return tiled
def _save_image(self):
if self.source_image is None:
QMessageBox.warning(self, '警告', '画像が読み込まれていません')
return
# デフォルトファイル名
grid_size = self.combo_grid.currentData()
base_name = Path(self.source_path).stem if self.source_path else 'output'
default_name = f'{base_name}_{grid_size}x{grid_size}.png'
path, _ = QFileDialog.getSaveFileName(
self, '保存先を選択', default_name,
'PNG画像 (*.png)'
)
if not path:
return
try:
tiled = self._create_tiled_image()
ok = save_image(path, tiled)
if not ok:
raise RuntimeError('save_image returned False')
QMessageBox.information(self, '完了', f'保存しました:\n{path}')
except Exception as e:
QMessageBox.warning(self, 'エラー', f'保存に失敗しました: {e}')
def main():
app = QApplication(sys.argv)
app.setApplicationName('Grid Tiler')
app.setStyle('Fusion')
app.setStyleSheet("""
QMainWindow { background-color: #1e1e1e; }
QWidget { background-color: #252526; color: #cccccc; }
QPushButton { background-color: #0e639c; color: white; border: none; padding: 5px 15px; border-radius: 3px; }
QPushButton:hover { background-color: #1177bb; }
QPushButton:pressed { background-color: #094771; }
QPushButton:disabled { background-color: #3c3c3c; color: #666; }
QGroupBox { border: 1px solid #3e3e42; margin-top: 10px; padding-top: 10px; }
QGroupBox::title { subcontrol-origin: margin; left: 10px; padding: 0 5px; }
QLabel { color: #cccccc; }
QComboBox { background-color: #3c3c3c; border: 1px solid #3e3e42; padding: 5px; min-width: 120px; }
QComboBox:hover { border: 1px solid #007acc; }
QComboBox::drop-down { border: none; }
QComboBox QAbstractItemView { background-color: #2d2d30; selection-background-color: #094771; }
""")
window = GridTilerWindow()
window.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()