読み込んだ画像を100100にresizeしたのですが、これを
1010の画像10個に分割したいのですが、その方法が分かりません。
Qiitaの
画像の分割と連結
https://qiita.com/ka10ryu1/items/015c6a6a5fa287a47828
という記事に、画像等分割のコードが書かれていたのですが、そのまま貼りつけても動きません。コードは以下になります。
python
1 import numpy as np 2 3 v_size = img.shape[0] // size * size 4 h_size = img.shape[1] // size * size 5 img = img[:v_size, :h_size] 6 7 v_split = img.shape[0] // size 8 h_split = img.shape[1] // size 9 out_img = [] 10 [out_img.extend(np.hsplit(h_img, h_split)) 11 for h_img in np.vsplit(img, v_split)]
出力結果として[None]が大量に出力されるだけです。
自分はGoogleColaboratoryで使いたいので、Colabでも使えるようにし、かつ回答を少し参考に一部いじったコードが以下です。
python
1from google.colab import drive 2drive.mount('/content/drive') 3 4import sys 5import numpy as np 6 7sys.path.append('/content/drive/My Drive') 8 9import ActivationFunction as AF 10from PIL import Image 11 12tefilename = "test2.png" 13teimg = Image.open("drive/My Drive/mnist_dataset/" + tefilename) 14teimg = teimg.resize((100, 100)) 15img = np.asarray(teimg) 16 17v_size = img.shape[0] 18h_size = img.shape[1] 19img = img[:v_size, :h_size] 20 21v_split = img.shape[0] 22h_split = img.shape[1] 23out_img = [] 24[out_img.extend(np.hsplit(h_img, h_split)) 25 for h_img in np.vsplit(img, v_split)] 26 27print(out_img)
出力結果は、
Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
[array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]], dtype=uint8), array([[[255, 255, 255]]],・・・(略)
となります。
たぶん、
python
1[out_img.extend(np.hsplit(h_img, h_split)) 2 for h_img in np.vsplit(img, v_split)]
の部分を、そもそも変更する必要があると思うのですが、どう変更すれば良いのか分かりません。
extendはリストに引数を追加するもので、これをリスト内に入れているのがおかしいと思うのですが、
ではforを用い、縦横分割はどのように記述すれば良いのか、よく分かりません。










回答1件
あなたの回答
tips
プレビュー