繁体   English   中英

Python 使用 pyinstaller 创建 EXE 后将图像转换为视频的脚本出现问题

[英]Problem with Python Script that converts images to video after creating EXE with pyinstaller

我想制作一个脚本,将存储在文件夹中的图像转换为视频。 这是代码:

    import cv2
    import numpy as np
    import os
    import pyautogui
    import msvcrt


    imageFolder = input('Please enter images folder path: ').replace(chr(34),"")
    outputPath = imageFolder+'\Video.avi'



    try:
        images = [img for img in os.listdir(imageFolder) if img.endswith(".jpg")]
        while len(images)==0:
            imageFolder = input('There are no images in the directory ! Please enter images folder path: ').replace(chr(34),"")
            images = [img for img in os.listdir(imageFolder) if img.endswith(".jpg")]

        print('Creating recording...')
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        frame = cv2.imread(os.path.join(imageFolder, images[0]))
        height, width, layers = frame.shape
        frameRate = 2

        video = cv2.VideoWriter(outputPath, fourcc, frameRate, (width,height))

        for image in images:
            print(f'{int((images.index(image)/len(images))*100)} %', end="\r")
            video.write(cv2.imread(os.path.join(imageFolder, image)))

        cv2.destroyAllWindows()
        video.release()

        decision = input('Recording has been created successfully ! Do you want to open it?  [Y/N]: ')
        if decision.lower() == 'y':
            print('Opening file...')
            os.startfile(outputPath)
    except:
        print(f'There was a problem with creating a recording. Check images path: {imageFolder}')

当我从命令行启动该代码时,该代码工作正常,但在使用 pyinstalller (pyinstaller -F ConvertToRecording.py) 将其转换为 EXE 后,我收到如下错误:

    [ERROR:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap.cpp (3
    92) cv::VideoWriter::open VIDEOIO(CV_IMAGES): raised OpenCV exception:          

    OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\videoio\src\cap_images.cp
    p:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the n
    ame of file): C:\Users\MyUser\Documents\Test\20191018_12_45\Video.avi in function 
    'cv::icvExtractPattern'          

任何帮助表示赞赏!

我遇到了同样的问题。 Just go to your OpenCV folder (if you don't have, go here: https://opencv.org/releases/ ) and find the opencv_videoio_ffmpeg420_64.dll ( I am using 4.20) file. 将其复制并粘贴到您的 exe 方向(同一文件夹)。

然后它将起作用。

使用os.path模块来处理路径而不是连接字符串。 这确保了更好的跨平台兼容性。 有关该模块的更详细说明,请参阅手册

暂无
暂无

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

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