Skip to content

Instantly share code, notes, and snippets.

@smeschke
Created December 5, 2022 13:56
Show Gist options
  • Save smeschke/e25be986bbaa595d19eb52c7aff5061f to your computer and use it in GitHub Desktop.
Save smeschke/e25be986bbaa595d19eb52c7aff5061f to your computer and use it in GitHub Desktop.
Python/OpenCV script that displays snow
import cv2
import numpy as np
import random
numSnowflakes = 2000
xSize, ySize = 1080, 1920
#cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
#cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
def new_snowflake():
xPosition = random.randint(0,ySize)
yPosition = 0
speed = random.random()+.01
size = random.randint(0,3)
c = random.randint(20,255)
color = c,c,c
snowflake = xPosition, yPosition, speed, size, color
return snowflake
def draw_snowflakes(bg, snowflakes):
for snowflake in snowflakes:
center = snowflake[0:2]
radius = snowflake[3]
color = snowflake[4]
cv2.circle(bg, tuple(np.array(center, int)), radius, color, -1)
return bg
def move_snowflakes(snowflakes):
newSnowflakes = []
for snowflake in snowflakes:
xPosition = snowflake[0]
yPosition = snowflake[1] + snowflake[2]
speed = snowflake[2] * 1.008
newSnowflake = xPosition, yPosition, speed, snowflake[3], snowflake[4]
if yPosition < ySize: newSnowflakes.append(newSnowflake)
else: newSnowflakes.append(new_snowflake())
return newSnowflakes
snowflakes = []
for i in range(numSnowflakes): snowflakes.append(new_snowflake())
while True:
bg = np.zeros((xSize, ySize, 3), np.uint8)
bg = draw_snowflakes(bg,snowflakes)
cv2.imshow('window', bg)
k = cv2.waitKey(1)
if k == 27: break
snowflakes = move_snowflakes(snowflakes)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment