win2xcur/win2xcur/main.py

64 lines
2.6 KiB
Python
Raw Permalink Normal View History

2020-09-26 18:14:55 -04:00
import argparse
import os
import sys
import traceback
2020-09-26 19:43:45 -04:00
from multiprocessing import cpu_count
from multiprocessing.pool import ThreadPool
from threading import Lock
2020-09-26 18:14:55 -04:00
2020-09-26 19:37:48 -04:00
from win2xcur import shadow
2020-09-26 18:14:55 -04:00
from win2xcur.parser import open_blob
from win2xcur.writer.x11 import check_xcursorgen, to_x11
def main() -> None:
parser = argparse.ArgumentParser(description='Converts Windows cursors to X11 cursors.')
parser.add_argument('files', type=argparse.FileType('rb'), nargs='+',
help='Windows cursor files to convert (*.cur, *.ani)')
parser.add_argument('-o', '--output', '--output-dir', default=os.curdir,
help='Directory to store converted cursor files.')
2020-09-26 19:37:48 -04:00
parser.add_argument('-s', '--shadow', action='store_true',
help="Whether to emulate Windows's shadow effect")
parser.add_argument('-O', '--shadow-opacity', type=int, default=50,
help='Opacity of the shadow (0 to 255)')
parser.add_argument('-r', '--shadow-radius', type=float, default=0.1,
help='Radius of shadow blur effect (as fraction of width)')
parser.add_argument('-S', '--shadow-sigma', type=float, default=0.1,
help='Sigma of shadow blur effect (as fraction of width)')
parser.add_argument('-x', '--shadow-x', type=float, default=0.05,
help='x-offset of shadow (as fraction of width)')
parser.add_argument('-y', '--shadow-y', type=float, default=0.05,
help='x-offset of shadow (as fraction of height)')
parser.add_argument('-c', '--shadow-color', default='#000000',
help='color of the shadow')
2020-09-26 18:14:55 -04:00
args = parser.parse_args()
2020-09-26 19:43:45 -04:00
print_lock = Lock()
2020-09-26 18:14:55 -04:00
check_xcursorgen()
2020-09-26 19:43:45 -04:00
def process(file):
2020-09-26 18:14:55 -04:00
name = file.name
blob = file.read()
try:
cursor = open_blob(blob)
except Exception:
2020-09-26 19:43:45 -04:00
with print_lock:
print('Error occurred while processing %s:' % (name,), file=sys.stderr)
traceback.print_exc()
2020-09-26 18:14:55 -04:00
else:
2020-09-26 19:37:48 -04:00
if args.shadow:
shadow.apply_to_frames(cursor.frames, color=args.shadow_color, radius=args.shadow_radius,
sigma=args.shadow_sigma, xoffset=args.shadow_x, yoffset=args.shadow_y)
2020-09-26 18:14:55 -04:00
result = to_x11(cursor.frames)
output = os.path.join(args.output, os.path.splitext(os.path.basename(name))[0])
with open(output, 'wb') as f:
f.write(result)
2020-09-26 19:43:45 -04:00
with ThreadPool(cpu_count()) as pool:
pool.map(process, args.files)
2020-09-26 18:14:55 -04:00
if __name__ == '__main__':
main()