-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransforms.py
More file actions
361 lines (305 loc) · 10.8 KB
/
Transforms.py
File metadata and controls
361 lines (305 loc) · 10.8 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import numpy as np
import cv2
import matplotlib.pyplot as plt
from src import Ruleset
SGL = 0.25
DBL = SGL * 2
HLF = SGL / 2
class ImageManipulation:
@staticmethod
def dump(image: cv2.typing.MatLike):
plt.figure(facecolor="black")
plt.grid(True, color="white", linewidth=0.5)
plt.gca().set_xticks(np.arange(0, image.shape[1], 16))
plt.gca().set_yticks(np.arange(0, image.shape[0], 16))
plt.gca().tick_params(length=0) # グリッドの目盛りを消す
plt.gca().set_aspect("equal") # 正方形グリッドを維持
plt.imshow(image)
plt.show()
@staticmethod
def empty_image(wh: tuple[int, int]) -> cv2.typing.MatLike:
(w, h) = wh
return np.zeros((h, w, 4), dtype=np.uint8)
@staticmethod
def empty_affine() -> cv2.typing.MatLike:
return np.array(
[
[0, 0, 0],
[0, 0, 0],
],
np.float32,
)
@staticmethod
def crop(
image: cv2.typing.MatLike, xy: tuple[int, int], size: int, dxdy: tuple[int, int]
) -> cv2.typing.MatLike:
(x, y) = xy
(dx, dy) = dxdy
return image[y + dy : y + dy + size, x + dx : x + dx + size]
@staticmethod
def load_image(path: str):
image = cv2.imread(path, cv2.IMREAD_UNCHANGED)
image = cv2.cvtColor(image, cv2.COLOR_BGR2BGRA)
return image
@staticmethod
def save_image(image: cv2.typing.MatLike, path: str):
cv2.imwrite(path, image)
@staticmethod
def apply(image: cv2.typing.MatLike, affine: cv2.typing.MatLike, size: int, r: int):
h, w = image.shape[:2]
# 平行移動
affine[0][2] *= w
affine[1][2] *= h
# 出力サイズに応じたリサイズ
affine *= size / h * r
output_size = (int(size * r), int(size * r))
return cv2.warpAffine(image, affine, output_size)
@staticmethod
def expand(image: cv2.typing.MatLike, r: int):
h, w = image.shape[:2]
output_size = (int(w * r), int(h * r))
return cv2.resize(image, output_size)
@staticmethod
def shrink(image: cv2.typing.MatLike, r: int, f: int):
h, w = image.shape[:2]
output_size = (int(w / r), int(h / r))
return cv2.resize(image, output_size, interpolation=f)
@staticmethod
def paste(
base: cv2.typing.MatLike,
overlay: cv2.typing.MatLike,
xy: tuple[int, int],
dxdy: tuple[int, int],
):
(x, y) = xy
(dx, dy) = dxdy
# 背景画像の指定された場所を切り出す
h, w = overlay.shape[:2]
roi = base[y + dy : y + dy + h, x + dx : x + dx + w]
# 背景画像と重ねる画像の各チャンネルを分離する
overlay_rgb = overlay[:, :, :3] # 重ねる画像のRGB
overlay_alpha = overlay[:, :, 3] # 重ねる画像のαチャンネル
base_rgb = roi[:, :, :3] # 背景画像の該当領域のRGB
base_alpha = roi[:, :, 3] # 背景画像の該当領域のαチャンネル
# αチャンネルを0〜1の範囲に正規化
alpha_overlay = overlay_alpha.astype(float) / 255.0
alpha_base = base_alpha.astype(float) / 255.0
# ベース画像が透明 (alpha_base == 0) の場合、overlay_rgbをそのまま使用する
blended_rgb = np.where(
alpha_base[..., None] == 0,
overlay_rgb,
alpha_overlay[..., None] * overlay_rgb
+ (1 - alpha_overlay[..., None]) * base_rgb,
).astype(np.uint8)
# アルファチャンネルのブレンド
blended_alpha = (alpha_overlay + alpha_base * (1 - alpha_overlay)) * 255
blended_alpha = blended_alpha.astype(np.uint8)
# 最終的な合成画像を作成
result = cv2.merge(
(
blended_rgb[:, :, 0],
blended_rgb[:, :, 1],
blended_rgb[:, :, 2],
blended_alpha,
)
)
# 合成した部分を背景画像に戻す
base[y + dy : y + dy + h, x + dx : x + dx + w] = result
return base
class ImageEdit:
def shift(
image: cv2.typing.MatLike, ruleset: Ruleset, args: list
) -> cv2.typing.MatLike:
# 画像がアルファチャンネルを持っているか確認
if image.shape[2] == 4:
# BGRとアルファチャンネルを分離
bgr = image[:, :, :3] # BGRチャンネル
alpha = image[:, :, 3] # アルファチャンネル
else:
bgr = image
alpha = None # アルファチャンネルがない場合
# BGRからHSVに変換
hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
# HSVチャンネルを分離
h, s, v = cv2.split(hsv)
hue_shift = args[0]
# OpenCVのHチャンネルは0〜179の範囲なので、360に対応させる
# Hチャンネルを0〜360に拡張して計算
h = (h.astype(np.int32) * 2 + hue_shift) % 360
# 360度の色相を再度0〜179の範囲にマッピング
h = (h // 2).astype(np.uint8)
# チャンネルを再結合
shifted_hsv = cv2.merge([h, s, v])
# HSVからBGRに変換
shifted_bgr = cv2.cvtColor(shifted_hsv, cv2.COLOR_HSV2BGR)
# アルファチャンネルがある場合、BGRと再結合
if alpha is not None:
# アルファチャンネルとBGRを結合して出力画像を作成
return cv2.merge([shifted_bgr, alpha])
else:
return shifted_bgr
def merge(
image: cv2.typing.MatLike, ruleset: Ruleset, args: list
) -> cv2.typing.MatLike:
path = args[0]
xy = (args[1] if len(args) > 1 else 0, args[2] if len(args) > 2 else 0)
dxdy = (args[3] if len(args) > 3 else 0, args[4] if len(args) > 4 else 0)
overlay = ImageManipulation.load_image(ruleset.resolve_path(path))
return ImageManipulation.paste(image, overlay, xy, dxdy)
def removeTransparent(
image: cv2.typing.MatLike, ruleset: Ruleset, args: list = []
) -> cv2.typing.MatLike:
specified_color = [255, 255, 231, 255] # BGRA形式
threshold = args[0] if len(args) > 0 else 128
alpha_channel = image[:, :, 3]
mask_below_threshold = alpha_channel < threshold
# 閾値未満の透明度を透過色にする
image[mask_below_threshold] = specified_color
image[:, :, 3] = 255
return image
def removeSpecial(
image: cv2.typing.MatLike, ruleset: Ruleset
) -> cv2.typing.MatLike:
# 置き換える色 (BGR形式)
color_map = (
((107, 107, 107), (107, 107, 106)),
((155, 155, 155), (155, 155, 154)),
((179, 179, 179), (179, 179, 178)),
((201, 201, 201), (201, 201, 200)),
((223, 223, 223), (223, 223, 222)),
((87, 101, 111), (87, 101, 110)),
((127, 155, 241), (127, 155, 240)),
((255, 255, 83), (255, 255, 82)),
((255, 33, 29), (255, 33, 28)),
((1, 221, 1), (1, 221, 0)),
((227, 227, 255), (227, 227, 254)),
((193, 177, 209), (193, 177, 208)),
((77, 77, 77), (77, 77, 76)),
((255, 1, 127), (255, 1, 126)),
((1, 1, 255), (1, 1, 254)),
((36, 75, 103), (36, 75, 102)),
((57, 94, 124), (57, 94, 123)),
((76, 113, 145), (76, 113, 144)),
((96, 132, 167), (96, 132, 166)),
((116, 151, 189), (116, 151, 188)),
((136, 171, 211), (136, 171, 210)),
((156, 190, 233), (156, 190, 232)),
((176, 210, 255), (176, 210, 254)),
((123, 88, 3), (123, 88, 2)),
((142, 111, 4), (142, 111, 3)),
((161, 134, 5), (161, 134, 4)),
((180, 157, 7), (180, 157, 6)),
((198, 180, 8), (198, 180, 7)),
((217, 203, 10), (217, 203, 9)),
((236, 226, 11), (236, 226, 10)),
((255, 249, 13), (255, 249, 12)),
)
for color in color_map:
# BGR形式の色をマスクに変換
mask = cv2.inRange(image[:, :, :3], np.array(color[0]), np.array(color[0]))
# 元のアルファ値を保持しながら色を置き換え
image[mask > 0, :3] = color[1]
# アルファ値はそのまま
image[mask > 0, 3] = image[mask > 0, 3]
return image
class Transforms:
"""
線形変換行列定義クラス
"""
@staticmethod
def keep():
"""
そのまま出力(正則行列)。resolutionによる拡大・縮小の影響は受ける。
"""
return np.array(
[
[1, 0, 0],
[0, 1, 0],
],
np.float32,
)
@staticmethod
def resize(r):
"""
リサイズ
"""
return np.array(
[
[r, 0, 0],
[0, r, 0],
],
np.float32,
)
@staticmethod
def reverse():
"""
タイル画像からテクスチャ画像を逆生成する
"""
return np.array(
[
[2, 4, -1.5],
[-2, 4, -0.5],
],
np.float32,
)
@staticmethod
def to_n():
"""
テクスチャの上をタイル画像の北へ向けて変換する
"""
return np.array(
[
[DBL, -DBL, DBL],
[SGL, SGL, DBL],
],
np.float32,
)
@staticmethod
def to_w():
"""
テクスチャの上をタイル画像の西へ向けて変換する
"""
return np.array(
[
[DBL, DBL, (DBL - DBL)],
[-SGL, SGL, (DBL + SGL)],
],
np.float32,
)
@staticmethod
def to_e():
"""
テクスチャの上をタイル画像の東へ向けて変換する
"""
return np.array(
[
[-DBL, -DBL, (DBL + DBL)],
[SGL, -SGL, (DBL + SGL)],
],
np.float32,
)
@staticmethod
def to_s():
"""
テクスチャの上をタイル画像の南へ向けて変換する
"""
return np.array(
[
[-DBL, DBL, DBL],
[-SGL, -SGL, (DBL + DBL)],
],
np.float32,
)
@staticmethod
def to_up(args: list[int] = []):
"""
テクスチャの右を緩坂1段分上げる
"""
height = args[0] if len(args) > 0 else 1
return np.array(
[
[0, 0, 0],
[0, HLF * height, -HLF * height],
],
np.float32,
)