回答編集履歴
2
修正
test
CHANGED
@@ -31,3 +31,63 @@
|
|
31
31
|
base64_ascii = binascii.b2a_base64(data).decode('ascii')
|
32
32
|
|
33
33
|
```
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
## base64 に変換できていることを確認したコード
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
Jupyter Notebook 上にインライン画像として img タグ表示して確認
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
```python
|
50
|
+
|
51
|
+
import cv2
|
52
|
+
|
53
|
+
import binascii
|
54
|
+
|
55
|
+
from IPython.display import HTML, display
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
# jpg の場合
|
60
|
+
|
61
|
+
img = cv2.imread("sample.jpg")
|
62
|
+
|
63
|
+
ret, jpg_data = cv2.imencode(".jpg", img, [cv2.IMWRITE_JPEG_QUALITY, 90])
|
64
|
+
|
65
|
+
base64_ascii = binascii.b2a_base64(jpg_data).decode("ascii")
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
img_tag = f'<img src="data:image/jpeg;base64,{base64_ascii}">'
|
70
|
+
|
71
|
+
display(HTML(img_tag))
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
# png の場合
|
76
|
+
|
77
|
+
img = cv2.imread("bird.png")
|
78
|
+
|
79
|
+
ret, png_data = cv2.imencode(".png", img)
|
80
|
+
|
81
|
+
base64_ascii = binascii.b2a_base64(png_data).decode("ascii")
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
img_tag = f'<img src="data:image/png;base64,{base64_ascii}">'
|
86
|
+
|
87
|
+
display(HTML(img_tag))
|
88
|
+
|
89
|
+
```
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
![イメージ説明](17894fc7ebb8c74b0e1fffaf6616bbb7.png)
|
1
修正
test
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
|
25
25
|
# ndarray -> binary
|
26
26
|
|
27
|
-
ret, data = cv2.imencode(".jpg", img, [
|
27
|
+
ret, data = cv2.imencode(".jpg", img, [cv2.IMWRITE_JPEG_QUALITY, 90])
|
28
28
|
|
29
29
|
# binary -> base64 ascii
|
30
30
|
|