回答編集履歴
1
a
answer
CHANGED
@@ -41,4 +41,42 @@
|
|
41
41
|
|
42
42
|
save_path = os.path.join(dirpath, os.path.basename(path))
|
43
43
|
img.save(save_path)
|
44
|
+
```
|
45
|
+
|
46
|
+
## 追記
|
47
|
+
|
48
|
+
白と黒のみで構成される画像は「2値画像」といいます。
|
49
|
+
|
50
|
+
```python
|
51
|
+
import glob
|
52
|
+
import os
|
53
|
+
import cv2
|
54
|
+
|
55
|
+
input_dirpath = 'tet' # 入力ディレクトリ
|
56
|
+
output_dirpath = 'output' # 出力ディレクトリ
|
57
|
+
|
58
|
+
# ファイルパスをピックアップ
|
59
|
+
types = ('*.png', '*.gif', '*.jpg')
|
60
|
+
img_paths = []
|
61
|
+
for t in types:
|
62
|
+
img_paths.extend(glob.glob(os.path.join(input_dirpath, t)))
|
63
|
+
|
64
|
+
# リサイズする。
|
65
|
+
for path in img_paths:
|
66
|
+
img = cv2.imread(path)
|
67
|
+
if img is None:
|
68
|
+
print('failed to loading image {}.'.format(path))
|
69
|
+
continue
|
70
|
+
|
71
|
+
# 分類する。
|
72
|
+
colors = np.unique(img.reshape(-1, img.shape[2]), axis=0)
|
73
|
+
if len(colors) <= 2 and np.all(np.logical_or(np.all(colors == [0, 0, 0], axis=1),
|
74
|
+
np.all(colors == [255, 255, 255], axis=1))):
|
75
|
+
dirpath = os.path.join(output_dirpath, 'binary')
|
76
|
+
else:
|
77
|
+
dirpath = os.path.join(output_dirpath, 'color')
|
78
|
+
|
79
|
+
os.makedirs(dirpath, exist_ok=True)
|
80
|
+
save_path = os.path.join(dirpath, os.path.basename(path))
|
81
|
+
cv2.imwrite(save_path, img)
|
44
82
|
```
|