“NoneType”对象没有属性“shape”,路径正确

2024-04-25 05:54:54 发布

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

我正在尝试在某个文件夹中拍摄所有照片,以.jpeg结尾,并使用opencv将其制作成视频。你知道吗

问题是在我试着得到图像形状它返回了一个错误,我在搜索答案时读了很多东西,几乎所有的人都这么说,路径不正确,所以我用os.path.isfile()函数测试了它,结果返回true,所以我不确定

def make_move(path):
    image_folder = path
    video_name = 'video.avi'
    images = [img for img in os.listdir(image_folder) if img.endswith(".jpeg")]
    frame = cv2.imread(image_folder + images[0], cv2.IMREAD_COLOR)

    print(os.path.isfile(image_folder + images[0]))# True
    print(frame) # prints None
    height, width, layers = frame.shape # Error here


    video = cv2.VideoWriter(video_name, 0, 1, (width,height))

    for image in images:
         video.write(cv2.imread(os.path.join(image_folder, image)))

    cv2.destroyAllWindows()
    video.release()

正如我提到的,即使路径是正确的,我也得到了非类型错误。你知道吗


Tags: pathnameimage路径imgforosvideo
1条回答
网友
1楼 · 发布于 2024-04-25 05:54:54

使用concat时,如果您在windows上,可能会缺少斜杠或反斜杠。例如:

image_folder = '/path/to/folder'
image_file = '1.jpg'

print(image_folder + image_file) # /path/to/folder1.jpg

print(os.path.join(image_folder, image_file) # /path/to/folder/1.jpg

而且os.path.join保证了整个系统的稳定性。你知道吗

相关问题 更多 >