-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom-image.py
73 lines (54 loc) · 1.99 KB
/
random-image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3.7
import asyncio
import iterm2
import os
import random
import re
# This script was created with the "basic" environment which does not support adding dependencies
# with pip.
# osascript "/Users/admin/Library/Application Support/iTerm2/Scripts/iterm2-random-background.scpt"
image_dir = "/Users/admin/Pictures/BackgroundImage"
images_temp = os.listdir(image_dir)
blend_re = re.compile("_blend\(([^)]*)\)")
def get_blend_value(s, default=0.30):
m = re.findall(blend_re, s)
if len(m) == 0:
return default
else:
return float(m[0])
images = list(
map(
lambda p: (os.path.join(image_dir, p), get_blend_value(p)),
filter(lambda s: not s.startswith("."), images_temp),
)
)
print(images)
async def main(connection):
app = await iterm2.async_get_app(connection)
async def change_pic(session_id, steps: int = 10, duration: float = 0.15):
session = app.get_session_by_id(session_id)
profile = await session.async_get_profile()
print("Session ID {} created".format(session_id))
p, blend = random.choice(images)
await profile.async_set_blend(0)
await profile.async_set_background_image_location(p)
async def change_blend(b: float):
b = max(0.0, min(1.0, b))
frame = duration / steps
i = 0
while i < steps:
s = asyncio.sleep(frame)
i += 1
await profile.async_set_blend((b * i) / steps)
await s
await change_blend(blend)
await asyncio.sleep(0.4)
await iterm2.EachSessionOnceMonitor.async_foreach_session_create_task(
app, lambda s: change_pic(s, steps=10, duration=0.1)
)
async with iterm2.NewSessionMonitor(connection) as mon:
while True:
session_id = await mon.async_get()
change_pic(session_id)
# This instructs the script to run the "main" coroutine and to keep running even after it returns.
iterm2.run_forever(main)