dotfiles/scripts/wastickers

56 lines
1.5 KiB
Python
Executable file

#!/bin/env python
import os
from shutil import copyfile, rmtree
import subprocess
from glob import glob
import time
import pathlib
from zipfile import ZipFile, ZIP_STORED
cwd = os.getcwd()
pathlib.Path(f'./tmp').mkdir(parents=True, exist_ok=True)
# taken from https://stackoverflow.com/a/34177358
def is_tool(name: str):
from shutil import which
return which(name) is not None
if is_tool('cwebp') == False:
print('cwebp not found; Make sure it is installed. Exiting.')
exit(1)
if is_tool('zip') == False:
print('zip not found; Make sure it is installed. Exiting.')
exit(1)
author = input('Author: ')
pname = input('Pack name: ')
with open(f'./tmp/title.txt', 'w') as f:
f.write(pname)
with open(f'./tmp/author.txt', 'w') as f:
f.write(author)
files = glob(f'./*.png') + glob(f'./*.jpg') + glob(f'./*.jpeg')
for file in files:
copyfile(file, f'./tmp/{file}')
os.chdir(f'{cwd}/tmp')
for file in files:
epoch = int(time.time())
print(f"Converting {file}...")
subprocess.run(['cwebp', '-q', '60', file, '-o', str(epoch) + '.webp'], stdout=open(os.devnull, 'w'), stderr=subprocess.STDOUT)
os.remove(f'./{file}')
time.sleep(1)
files = glob('./*')
with ZipFile(f'{pname}.wastickers', 'w', ZIP_STORED) as zip:
for file in files:
zipfilename = file.replace('./', '/')
with open(file, 'rb') as image_file:
zip.writestr(f"{zipfilename}", image_file.read())
os.chdir(cwd)
copyfile(f'./tmp/{pname}.wastickers', f'./{pname}.wastickers')
rmtree('./tmp')