如何保存np.tile图像

2024-04-23 18:28:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想获得尺寸为60*25*25*1的平铺图像,并将它们保存在与原始图像同名的文件夹中。目前,我得到60*25*25形状的图像。最后,我想用这些图像形成大小为[batchsize,60,25,25,1]的张量,以输入到我的模型中

有人能帮我吗

我正在使用的代码


outPath = "C:/output-images/"
path = "C:/input-images"

# iterate through the names of contents of the folder
for image_path in os.listdir(path):

    # create the full input path and read the file
    input_path = os.path.join(path, image_path)
    image_to_tile = np.array(Image.open(input_path))
    print(image_to_tile.shape)
    # rotate the image
    tiled = np.tile(image_to_tile, [60,1,1])
    print(tiled.shape)
    # create full output path, 'example.jpg' 
    # becomes 'rotate_example.jpg', save the file to disk
    fullpath = os.path.join(outPath, 'tiled-' + image_path)
    cv2.imwrite(fullpath, tiled)

输出:

(25, 25)
(60, 25, 25)
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-85-8a74569fa0d7> in <module>
     15     # becomes 'rotate_example.jpg', save the file to disk
     16     fullpath = os.path.join(outPath, 'tiled-' + image_path)
---> 17     cv2.imwrite(fullpath, tiled)

error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-zsozjuva\opencv\modules\imgcodecs\src\loadsave.cpp:674: error: (-215:Assertion failed) image.channels() == 1 || image.channels() == 3 || image.channels() == 4 in function 'cv::imwrite_'


Tags: thetopathin图像imageinputos