Using Python instead of Bash
Ethan Furman
ethan at stoneleaf.us
Mon Jun 1 01:08:06 EDT 2015
Just for funsies, I did a slight rewrite using my scription[1] library:
--8<------------------------------------------------------------------------------
#!/usr/bin/python3
"""
Convert scanned images: contrast is increased, size is enlarged,
format is changed from jpeg to png.
"""
from __future__ import print_function # only needed if running in 2
from glob import glob
from scription import *
@Command(
threshold=Spec('cutoff value for white vs black', OPTION, type=int, default=66),
verify=Spec('confirm conversion by viewing on screen', FLAG),
)
def image_convert(verify, threshold):
for count, input in enumerate(sorted(glob('*.JPG')), start=1):
output = '{0:02d}.png'.format(count)
# only prints if --verbose on commandline
print('Going to convert {} to {}'.format(input, output))
while True:
# Execute automatically parses into a list
attempt = Execute(
'convert -threshold {}% {} {}'.format(threshold, input, output),
interactive='echo',
)
if attempt.returncode:
raise SystemExit(attempt.returncode)
if not verify:
break
attempt = Execute(
'display_the_png {}'.format(output),
interactive='echo',
)
if attempt.returncode:
raise SystemExit(attempt.returncode)
if get_response('Does the image look good?'):
break
threshold = get_response("New threshold value:", type=int)
# conversion looks good, or manual verification skipped
print('going to print {0}'.format(output))
attempt = Execute(
'lpr -o fit-to-page -o media=A4 {}'.format(output),
interactive='echo',
)
if attempt.returncode:
raise SystemExit(attempt.returncode)
Main()
--8<------------------------------------------------------------------------------
Opinions about the usability of the above script, both as the script writer and as the user, welcomed.
--
~Ethan~
[1] yes, everyone apparently writes their own command-line processor -- this one is mine. ;)
More information about the Python-list
mailing list