回答編集履歴
1
a
test
CHANGED
@@ -85,3 +85,79 @@
|
|
85
85
|
img.save(save_path)
|
86
86
|
|
87
87
|
```
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
## 追記
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
白と黒のみで構成される画像は「2値画像」といいます。
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
```python
|
100
|
+
|
101
|
+
import glob
|
102
|
+
|
103
|
+
import os
|
104
|
+
|
105
|
+
import cv2
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
input_dirpath = 'tet' # 入力ディレクトリ
|
110
|
+
|
111
|
+
output_dirpath = 'output' # 出力ディレクトリ
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
# ファイルパスをピックアップ
|
116
|
+
|
117
|
+
types = ('*.png', '*.gif', '*.jpg')
|
118
|
+
|
119
|
+
img_paths = []
|
120
|
+
|
121
|
+
for t in types:
|
122
|
+
|
123
|
+
img_paths.extend(glob.glob(os.path.join(input_dirpath, t)))
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
# リサイズする。
|
128
|
+
|
129
|
+
for path in img_paths:
|
130
|
+
|
131
|
+
img = cv2.imread(path)
|
132
|
+
|
133
|
+
if img is None:
|
134
|
+
|
135
|
+
print('failed to loading image {}.'.format(path))
|
136
|
+
|
137
|
+
continue
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
# 分類する。
|
142
|
+
|
143
|
+
colors = np.unique(img.reshape(-1, img.shape[2]), axis=0)
|
144
|
+
|
145
|
+
if len(colors) <= 2 and np.all(np.logical_or(np.all(colors == [0, 0, 0], axis=1),
|
146
|
+
|
147
|
+
np.all(colors == [255, 255, 255], axis=1))):
|
148
|
+
|
149
|
+
dirpath = os.path.join(output_dirpath, 'binary')
|
150
|
+
|
151
|
+
else:
|
152
|
+
|
153
|
+
dirpath = os.path.join(output_dirpath, 'color')
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
os.makedirs(dirpath, exist_ok=True)
|
158
|
+
|
159
|
+
save_path = os.path.join(dirpath, os.path.basename(path))
|
160
|
+
|
161
|
+
cv2.imwrite(save_path, img)
|
162
|
+
|
163
|
+
```
|