initial commit
This commit is contained in:
78
src/main.py
Executable file
78
src/main.py
Executable file
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Akbar Rahman <hi@alv.cx>
|
||||
#
|
||||
|
||||
import shlex
|
||||
import sys
|
||||
import subprocess
|
||||
import time
|
||||
import tomllib
|
||||
|
||||
from PIL import Image, ImageDraw, BdfFontFile
|
||||
|
||||
|
||||
def get_args():
|
||||
""" Get command line arguments """
|
||||
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-c", "--config", type=str, default="config.toml")
|
||||
parser.add_argument("-L", "--no-loop", action="store_true")
|
||||
return parser.parse_args()
|
||||
|
||||
def generate(config):
|
||||
if config.get('template'):
|
||||
img = Image.open(config['template'])
|
||||
else:
|
||||
img = Image.new(mode="RGBA", size = config['size'])
|
||||
|
||||
draw = ImageDraw.Draw(img)
|
||||
with open(config['font'], "rb") as fp:
|
||||
font = BdfFontFile.BdfFontFile(fp).to_imagefont()
|
||||
|
||||
process = subprocess.run(config['command'], stdout=subprocess.PIPE, shell=False)
|
||||
if process.returncode != config.get('return_code', 0):
|
||||
# process did not run succesfully.
|
||||
# do not risk displaying that to user
|
||||
return
|
||||
|
||||
text = process.stdout.decode('utf-8').split('\n')[0]
|
||||
for filter in config.get('text_filters', []):
|
||||
if filter == "lowercase":
|
||||
text = text.lower()
|
||||
elif filter == "uppercase":
|
||||
text = text.upper()
|
||||
|
||||
draw.text(config.get('text_offset', [0, 0]), text, font=font)
|
||||
|
||||
img.save(config['output'], save_all=True)
|
||||
|
||||
|
||||
def main(args):
|
||||
""" Entry point for script """
|
||||
with open(args.config, "rb") as f:
|
||||
config = tomllib.load(f)
|
||||
|
||||
print(f"{config=}")
|
||||
|
||||
while True:
|
||||
for c in config['image']:
|
||||
generate(c)
|
||||
|
||||
if args.no_loop:
|
||||
break
|
||||
|
||||
if not config.get('loop', {}).get('enable'):
|
||||
break
|
||||
|
||||
time.sleep(config.get('loop', {}).get('sleep', 60))
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
sys.exit(main(get_args()))
|
||||
except KeyboardInterrupt:
|
||||
sys.exit(0)
|
||||
Reference in New Issue
Block a user