Fox__
Fox__

Reputation: 21

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

I wanted to make a script that will convert images stored in a folder to video. Here's the code:

    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}')

The code works fine when I'm launching that from command line, but after converting that to EXE with pyinstalller (pyinstaller -F ConvertToRecording.py) I'm getting an error like this:

    [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'          

Any help appreciated !

Upvotes: 1

Views: 349

Answers (2)

Gavin Xu
Gavin Xu

Reputation: 73

I met the same problem. 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. copy it and paste it to your exe direction (same folder).

Then it will work.

Upvotes: 1

agtoever
agtoever

Reputation: 1699

Use the os.path module to with paths instead of concatenating strings. This ensures a better cross-platform compatibility. See the manual for a more elaborate explanation of the module.

Upvotes: 0

Related Questions