回答編集履歴

2

2018/10/18 05:34

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -1,4 +1,12 @@
1
+ Windows 上では OpenCV で日本語のファイル名を読み込めなかったため、PIL を使いました。
2
+
3
+ もし PIL が入っていなければ、以下で入れてください。
4
+
5
+ ```
6
+
1
- こんな感じでしょうか?
7
+ pip install pillow
8
+
9
+ ```
2
10
 
3
11
 
4
12
 
@@ -10,7 +18,7 @@
10
18
 
11
19
 
12
20
 
13
- import cv2
21
+ from PIL import Image
14
22
 
15
23
 
16
24
 
@@ -24,7 +32,7 @@
24
32
 
25
33
  # ファイルパスをピックアップ
26
34
 
27
- types = ('*.png', '*.jpg')
35
+ types = ('*.png', '*.gif', '*.jpg')
28
36
 
29
37
  img_paths = []
30
38
 
@@ -32,7 +40,7 @@
32
40
 
33
41
  img_paths.extend(glob.glob(os.path.join(input_dirpath, t)))
34
42
 
35
-
43
+
36
44
 
37
45
  # ref: https://stackoverflow.com/questions/27366479/python-3-os-walk-file-paths-unicodeencodeerror-utf-8-codec-cant-encode-s
38
46
 
@@ -48,7 +56,7 @@
48
56
 
49
57
  print(path)
50
58
 
51
- img = cv2.imread(path)
59
+ img = Image.open(path)
52
60
 
53
61
  if img is None:
54
62
 
@@ -56,14 +64,14 @@
56
64
 
57
65
  continue
58
66
 
59
- img = cv2.resize(img, size)
67
+ img = img.resize(size)
60
68
 
61
- print('resized image {} size: {} -> {}.'.format(path, img.shape, size))
69
+ print('resized image {} size: {} -> {}.'.format(path, img.size, size))
62
70
 
63
71
 
64
72
 
65
73
  save_path = os.path.join(output_dirpath, os.path.basename(path))
66
74
 
67
- cv2.imwrite(save_path, img)
75
+ img.save(save_path)
68
76
 
69
77
  ```

1

a

2018/10/18 05:34

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -11,8 +11,6 @@
11
11
 
12
12
 
13
13
  import cv2
14
-
15
- import matplotlib.pyplot as plt
16
14
 
17
15
 
18
16
 
@@ -32,7 +30,13 @@
32
30
 
33
31
  for t in types:
34
32
 
35
- img_paths.extend(glob.glob(os.path.join(dirpath, t)))
33
+ img_paths.extend(glob.glob(os.path.join(input_dirpath, t)))
34
+
35
+
36
+
37
+ # ref: https://stackoverflow.com/questions/27366479/python-3-os-walk-file-paths-unicodeencodeerror-utf-8-codec-cant-encode-s
38
+
39
+ img_paths = [path.encode('utf8','surrogateescape').decode('utf8','surrogateescape') for path in img_paths]
36
40
 
37
41
 
38
42
 
@@ -41,6 +45,8 @@
41
45
  os.makedirs(output_dirpath, exist_ok=True)
42
46
 
43
47
  for path in img_paths:
48
+
49
+ print(path)
44
50
 
45
51
  img = cv2.imread(path)
46
52
 
@@ -54,7 +60,7 @@
54
60
 
55
61
  print('resized image {} size: {} -> {}.'.format(path, img.shape, size))
56
62
 
57
-
63
+
58
64
 
59
65
  save_path = os.path.join(output_dirpath, os.path.basename(path))
60
66