Advertisement
Guest User

EveryFrame.py

a guest
Dec 21st, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #If this is going to be legible I should probably start with a top level description of what it is doing. Basically, it keeps generating random frames, and if that frame is not already in the video.
  2.  
  3. import cv2 as cv
  4. import numpy as np
  5. import random
  6.  
  7. #this function is more complicated than the rest of the program (which I think is good coding). You should read the comments on it last.
  8. def findNewFrame():
  9. guess = np.zeros((height,width,3), dtype=np.uint8) #so here I'm making a blank image of the correct size
  10. for i in range(height): #and then this nested for loop randomizes each pixel in the image
  11. for j in range(width):
  12. guess[i][j]=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
  13. isOld=False
  14. for frame in frames: #then this for loop goes through the list of frames that have been used, and makes sure the random image I've generated isn't the same as any of them
  15. if(frame.shape == guess.shape and not(np.bitwise_xor(frame,guess).any())):
  16. isOld=True
  17. if(isOld): #and then this is the part that is hardest to explain if you don't know any coding, but basically I am using a recursive call to say "if it turns out the random frame I generated is one that's already been used, try again"
  18. guess = findNewFrame()
  19. return guess #and then once it has successfully found a new frame, it will return that
  20.  
  21. #Here I'm defining just defining the dimensions of the movie, in this case those of my laptop
  22. height = 1080
  23. width = 1920
  24.  
  25. #Initiating a video to write to
  26. fourcc = cv.VideoWriter_fourcc(*'DIVX')
  27. film = cv.VideoWriter('film.mkv', fourcc, 24, (width, height))
  28.  
  29. #Making an empty list, which each frame I use can be added to, and then doing the math to determine how many frames total there should be
  30. frames = []
  31. allFrames=height*width*256*256*256
  32.  
  33. while len(frames)<(allFrames-1): #this is starting a loop that will continue until the list of frames that have been used contains all the possible frames
  34. #and then in this loop I find a new frame (see the function defined above, which has to be at the top because it has to be above the point at which it is called) then add it to the list of frames that have been used and add it to the video. Finally I print the number of frames that have been used so far to the console, mostly so I can see that it is actually making progress.
  35. newFrame = findNewFrame()
  36. frames.append(newFrame)
  37. film.write(newFrame)
  38. print(len(frames))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement