简体   繁体   English

如何将4D Numpy数组保存到图像

[英]How to save 4d numpy array to images

I want to create an image date_set which includes 176 small images (128*128*3) from one big image (1408, 2048, 3). 我想创建一个图像date_set,其中包括一个大图像(1408、2048、3)中的176个小图像(128 * 128 * 3)。 I do the following thing: step 1. Load the big image and convert it to numpy array. 我执行以下操作:步骤1.加载大图像并将其转换为numpy数组。 (1408, 2048, 3) 3d array step 2. cut it into 176 pieces: (176, 128, 128, 3) 4d array step 3. I don't know how to save 176 images from 4d array in this step. (1408,2048,3)3d数组第2步。将其切成176个片段:(176,128,128,3)4d数组第3步。在此步骤中,我不知道如何保存4d数组中的176张图像。 Does anyone could help me to solve this problem? 有谁能帮助我解决这个问题? Thanks very much! 非常感谢!

from astropy.io import fits
from astropy.utils.data import download_file
image_file = download_file('https://data.sdss.org/sas/dr12/boss/photoObj/frames/301/1035/3/frame-irg-001035-3-0011.jpg', cache=True )
image = imread(image_file)


def blockshaped(arr, nrows, ncols, c):
"""
Return an array of shape (n, nrows, ncols) where
n * nrows * ncols = arr.size

If arr is a 2D array, the returned array should look like n subblocks with
each subblock preserving the "physical" layout of arr.
"""
h, w = arr.shape[:2]
return (arr.reshape(h//nrows, nrows, -1, ncols, c)
           .swapaxes(1,2)
           .reshape(-1, nrows, ncols, c))

a= image[:1408, :]
b= blockshaped(a, 128, 128, 3)
b.shape

b.shape = (176, 128, 128, 3) b.shape =(176,128,128,3)

Here's a possible way to do it. 这是一种可行的方法。

import numpy as np
import scipy.misc

images = np.zeros((176,128,128,3)) 
for i in range(len(images)):
    scipy.misc.imsave('date_set_' + str(i) + '.jpg', images[i,:,:,:])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM