使用for循环将numpy文件的每个切片转换为jpeg格式

byqmnocz  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(124)

我是python的初学者,我有一个numpy文件,形状为(200,256,256)。我想用jpeg格式保存每个切片(:,256,256)。我如何使用for循环?提前感谢
我试过这个方法,但是对于形状为(256,256)的图像
your text from PIL import图像your text import numpy as np
your text A= np.load('image.npy',allow_pickle = True)your text im = Image.fromarray(A)your textim.save(“image.jpeg”)
但是我想用for循环来处理一个形状为(200,256,256)的文件,因为我有一些这样的文件。

v2g6jxz6

v2g6jxz61#

您可以在第一维上循环,提取图像切片并保存它们

from PIL import Image
import numpy as np

A = np.load('image.npy' ,allow_pickle = True)
for i in range(A.shape[0]):
    im = Image.fromarray(A[i,:,:])
    im = im.convert("L")
    im.save(f"image_{i}.jpeg")

图像将保存为image_0.jpegimage_1.jpeg

相关问题