2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

jpg形式の画像ファイルをpythonでnumpyに読み込む

Posted at

やりたいこと

  • jpg形式の画像ファイルをnumpyに読み込みたい。

画像ファイル

任意のグラフィックソフトなどを用いて事前に作成しておく。
ここでは下記の input_label_2.jpg を作成した。width:28px height:28px

image.png

実装

from PIL import Image
import numpy as np

input_file_name = 'input_label_2.jpg'
im = np.array(Image.open(input_file_name))
im_gray = np.array(Image.open(input_file_name).convert('L'))
print(im_gray)

出力

[[  0   0   0   0   0   0   0   0   0   0   0   0   1   1   0   0   0   1    1   0   0   0   1   4   0   0   0   0]
 [  0   0   0   0   0   0   0   0  14   5   0   0   1   5   3   0   0   1    4   7   7   4   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   4   7   5   1   0   0  12   5    0   0   0   0   2   3   0   0   0   0]
 [  0   0   0   0   0   0   0   0  14  12   7   0   0   0   3   8   0   0    0   5   8   9   6   3   0   0   0   0]
 [  0   0   0   0   0   0   0   0   2   0   0   0   1   4   2   0   4   5    4   1   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   3   8   6   0   0   0   5   0   0    0   2   7  12  16  18   0   0   0   0]
 [  0   0   0   0   0   0   0   0   3   0   0   0  13  33  55  69   9   0    0   0   0   4   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   5   3  33 111 205 255 255 237 242 180   94  28   0   0   5   8   0   0   0   0]
 [  0   0   0   0   0   0   0   0   7  47 241 255 231 174 141 117 238 246  250 188  15   1   1   8   0   0   0   0]
 [  0   0   0   0   0   0   0   0  24 246 243 242 120   3   0  17   0 207  254 253 162   0  20   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0  72 255 250 180   0   0  15   0  15  68  255 243 230   3   7  11   0   0   0   0]
 [  0   0   0   0   0   0   0   0 155 237 252 124  12  21   0   5   0 130  255 238 209   0   0  12   0   0   0   0]
 [  0   0   0   0   0   0   0   0  64  77  69   0   0   1   8   0  16 225  245 255 107   0   5   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   4   2  10   5  11  28 213 242  246 172   3   7  14   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0  20  14   0   0   0   9  48 241 255 250  174  26   1   0  10   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   1   1  11  11 157 251 255 249 106    4   0  12  14   0   5   0   0   0   0]
 [  0   6   3   1   5   2   0   2   2   1   2  49 216 254 255 219  52  10    0   7   0   0  12   2   0   0   0   0]
 [  3  10   1   0   0   7   7  10   2  14 106 255 239 253 161  10   4   0    0   6   2   0   1   0   0   0   0   0]
 [  0   9   5   0   3   5   0   0   9 156 255 238 237  77   8   0   6   5    0   0   0   0   5  17   0   0   0   0]
 [  0   5   4   5   7   0   0   8 185 251 246 231  57  13   0  10   0   6    0   5  15   0   0  10   0   0   0   0]
 [  8   0   0   1   4   0  16  54 252 255 254 251 237 232 252 245 215 219  208 204 179  91  14  11   0   0   0   0]
 [  5   0   0   4   6   0  11  50 123 135 129 137 131 121 121 114 133 135  127 134 125  55   0   0   0   0   0   0]
 [  0   2   3   6   8   0   0  10   0   9   0   0   7   3   3   7   0   2    0   0  13   3   0  12   0   0   0   0]
 [  0   9   4   0   1   5   2   1   6   0   7   2  13   0  15   0   0   7    4   0   1   3   0   1   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0]
]

image.png

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?