Video-Summary/generate test footage/gen.py

77 lines
1.8 KiB
Python
Raw Normal View History

import math
from PIL import Image, ImageDraw
import random
2020-09-19 13:21:07 +00:00
import imageio
import glob
import os
def getRandomColorString():
return '#{:06x}'.format(random.randint(0, 256**3))
fps = 30
xmax = 1920
ymax = 1080
# in minutes
2020-09-19 13:21:07 +00:00
length = .1
numberOfEvents = 3
2020-09-19 13:21:07 +00:00
dirname = os.path.dirname(__file__)
2020-09-19 21:13:44 +00:00
imageType = ".png"
2020-09-19 13:21:07 +00:00
imagesPath = os.path.join(dirname, 'images')+"/"
outputPath = os.path.join(dirname, 'out.mp4')
# creating new Image object
2020-09-19 13:21:07 +00:00
def genImages():
counter = 0
for i in range(numberOfEvents):
objectWidth = (5 + random.randint(0, 5)) * xmax / 100
objectHeight = (10 + random.randint(-5, 5)) * ymax / 100
objectX = random.randint(0, xmax)
objectY = random.randint(0, ymax)
objectSpeedX = random.randint( 1 ,5 )
objectSpeedY = random.randint( 1, 5 )
color = getRandomColorString()
for j in range(int(fps*length*60 / numberOfEvents)):
counter+=1
objectX -= objectSpeedX
objectY -= objectSpeedY
objectShape = [(objectX, objectY), (objectX + objectWidth, objectY + objectHeight)]
img = Image.new("RGB", (xmax, ymax))
# create rectangle image
img1 = ImageDraw.Draw(img)
img1.rectangle(objectShape, fill = color)
2020-09-19 21:13:44 +00:00
img.save( imagesPath + str(counter).zfill(6) + imageType)
2020-09-19 13:21:07 +00:00
def makeVideo():
fileList = []
for file in sorted(os.listdir(imagesPath)):
complete_path = imagesPath + file
fileList.append(complete_path)
writer = imageio.get_writer(outputPath, fps=fps)
for im in fileList:
writer.append_data(imageio.imread(im))
writer.close()
2020-09-19 21:13:44 +00:00
def deleteImages():
filelist = glob.glob(os.path.join(imagesPath, "*" + imageType))
for f in filelist:
os.remove(f)
2020-09-19 13:21:07 +00:00
genImages()
makeVideo()
2020-09-19 21:13:44 +00:00
#deleteImages()