open cvで画像をファイルごと読み込み後,
28*28のshapeで分割し,それを一枚ずつjpgとして保存したいのですがわかりません
以下実行した文です
import sys, os
sys.path.append(os.pardir) # 親ディレクトリのファイルをインポートするための設定
import numpy as np
from PIL import Image
import glob
import cv2
import matplotlib.pyplot as plt
def img_show(img):
pil_img = Image.fromarray(np.uint8(img))
pil_img.show()
data = []
for file in glob.glob('./sand/*jpg'):
print(file)
img = (cv2.imread(file, 1))
cv2.imshow('sand', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
v_size = img.shape[0]//28*28 h_size = img.shape[1]//28*28 img = img[:v_size, :h_size] v_split = img.shape[0]//28 h_split = img.shape[1]//28 out_img = [] [out_img.extend(np.hsplit(h_img, h_split))for h_img in np.vsplit(img, v_split)] im = np.array(out_img) print(im.dtype) # データ型 print(im.ndim) # 次元数 print(im.shape) im = im.reshape(28, 28) print(im.shape) print(im)
img_show(im)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。