Skip to content

Instantly share code, notes, and snippets.

@CarlosGS
Last active October 14, 2019 11:54
Show Gist options
  • Save CarlosGS/e2f4dfa5fd4297fd0312 to your computer and use it in GitHub Desktop.
Save CarlosGS/e2f4dfa5fd4297fd0312 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Generates and sets a random background representing a simulated Levy flight (https://en.wikipedia.org/wiki/Levy_flight)
# of 5 robots (starting at the center of the display)
# You can add the following lines to .bashrc so the wallpaper is updated everytime a terminal is opened
# python /home/carlosgs/wallpaper/generate_background.py & # Refresh the wallpaper in a background process
# BG_WP_PID=$!
# disown $BG_WP_PID # Hide stdout output from the previous command
# Begin modules
import numpy as np
import cv2
import os
# End modules
np.random.seed() # Set random seed using system time
# Units: pixels
margin = 100
size_X = 1440
size_Y = 900
max_search_distance = size_X*10
min_step_size = 1
img = np.ones((size_Y,size_X,3), np.uint8)
img *= 128
centreX = size_X/2
centreY = size_Y/2
cv2.line(img, (margin,margin), (size_X-margin,margin), (0,0,0))
cv2.line(img, (margin,margin), (margin,size_Y-margin), (0,0,0))
cv2.line(img, (margin,size_Y-margin), (size_X-margin,size_Y-margin), (0,0,0))
cv2.line(img, (size_X-margin,size_Y-margin), (size_X-margin,margin), (0,0,0))
def tuple_lerp(start,end,l):
start = np.array(start)
end = np.array(end)
res = start*(1-l) + end*l
return tuple(res)
for color in [(0,0,0), (255,0,0), (0,255,0), (0,0,255), (255,255,255)]:
remaining_distance = max_search_distance
alpha = 1.5 #+ np.random.normal(0,scale=0.5)
minDistance = min_step_size
#levy_evals = np.power(np.random.random(N_evaluations),(-1./alpha)) * minDistance
N_evaluations = 0
levy_evals = []
while remaining_distance > 0:
l = np.power(np.random.random(),(-1./alpha)) * minDistance
levy_evals.append(l)
N_evaluations += 1
remaining_distance -= l
newX = centreX
newY = centreY
prevX = newX
prevY = newY
theta = 0
#theta_trend = 0
for i in range(N_evaluations):
progress = float(i)/float(N_evaluations)
r = levy_evals[i]
if (prevX > margin) and (prevX < size_X-margin) and (prevY > margin) and (prevY < size_Y-margin):
theta += np.random.random()*np.pi
#if r > 50:
#theta = np.random.normal(theta_trend, scale=np.pi/2)
else:
theta = np.random.normal(loc=np.arctan2(centreY-prevY,centreX-prevX), scale=np.pi/2)
theta %= 2.*np.pi
#if r > 20:
#theta_trend = theta_trend*0.9 + theta*0.1
newX = prevX + r*np.cos(theta)
newY = prevY + r*np.sin(theta)
cv2.line(img, (int(prevX),int(prevY)), (int(newX),int(newY)), tuple_lerp((128,128,128),color,progress), thickness=min_step_size, lineType=cv2.CV_AA)
prevX = newX
prevY = newY
img *= 0.7 # darken the image a little bit
userdir = os.path.expanduser("~")
if not os.path.exists(userdir+'/levywallpaper/'):
os.makedirs(userdir+'/levywallpaper/')
import datetime
ts = str(datetime.datetime.now())
bgfile = userdir+'/levywallpaper/'+ts+'.png'
cv2.imwrite(bgfile,img)
from gi.repository import Gio
SCHEMA = 'org.gnome.desktop.background'
KEY = 'picture-uri'
def change_background(filename):
gsettings = Gio.Settings.new(SCHEMA)
gsettings.set_string(KEY, 'file://' + filename)
gsettings.apply()
change_background(bgfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment