Skip to content

Instantly share code, notes, and snippets.

@ch3ll0v3k

ch3ll0v3k/main.simple.2.py

Last active Jun 24, 2019
Embed
What would you like to do?
# pip install pillow opencv-python opencv-contrib-python
import os
import sys
import cv2
from PIL import Image
from time import sleep
import urllib.request
import requests
# ---------------------------------------------------------------------
CODEC = 0; # cv2.VideoWriter_fourcc('H','2','6','4');
VIDEO_NAME = 'myVideo.avi';
IMAGE_FOLDER = 'images';
FPS = 10;
PROTOHOST = 'https://www.gunnerkrigg.com/comics/00000002.jpg'; # 'http://127.0.0.1/....';
PROTOHOST = 'https://sample-videos.com/img/Sample-jpg-image-100kb.jpg'; # 'http://127.0.0.1/....';
PROTOHOST = 'https://cdn.pixabay.com/photo/2016/09/01/10/23/image-1635747_960_720.jpg'; # 'http://127.0.0.1/....';
# PROTOHOST = 'http://192.168.0.170/cgi-bin/video.jpg';
VIDEO_LENGTH_SEC = 5;
def getAllImages():
return [img for img in os.listdir(IMAGE_FOLDER)
if img.endswith(".jpg") or
img.endswith(".jpeg") or
img.endswith("png")]
def generate_video():
print( ' ------------------------ ' );
print( ' Creating video ' );
try:
images = getAllImages();
images.sort();
frame = cv2.imread(os.path.join(IMAGE_FOLDER, images[0]))
height, width, layers = frame.shape
video = cv2.VideoWriter(VIDEO_NAME, CODEC, FPS, (width, height));
for image in images:
video.write(cv2.imread(os.path.join(IMAGE_FOLDER, image)))
cv2.destroyAllWindows()
video.release();
print( ' Creating video: Done ... ' );
except Exception as e:
print( ' #Warning: '+str(e)+"\n" );
sleep( 1 );
# ---------------------------------------------------------------------
def main():
# Remove old images
images = getAllImages();
for image in images:
os.remove( os.path.join(IMAGE_FOLDER, image) );
current_second = 0;
total_frames = 0;
session = requests.Session();
session.trust_env = False;
while current_second < VIDEO_LENGTH_SEC:
print( ' ------------------------ ' );
print( ' '+str(current_second)+' of '+str(VIDEO_LENGTH_SEC)+' seconds ...' );
current_frame = 0;
while current_frame < FPS:
print( ' #current_frame: ['+str(current_frame)+']' );
new_image = os.path.join('.', IMAGE_FOLDER, str( total_frames )+'.jpg');
# urllib.request.urlretrieve( PROTOHOST, new_image );
# response = requests.get( PROTOHOST );
response = session.get( PROTOHOST, params={'pretty':'true'} );
if response.status_code == 200:
with open( new_image, 'wb') as f:
f.write(response.content);
sleep( 1 / FPS );
current_frame += 1;
total_frames += 1;
current_second += 1;
generate_video();
exit();
# ---------------------------------------------------------------------
if __name__ == '__main__':
while True:
try:
main()
except KeyboardInterrupt as e:
# CTRL+C => exit ...
print(' Exit ... ');
exit();
except Exception as e:
print( e );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment