Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; '"""': 0.05; 'main()': 0.07; 'option,': 0.07; 'rewrite': 0.07; '__future__': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'input,': 0.09; 'message-id:@stoneleaf.us': 0.09; 'script,': 0.09; 'threshold': 0.09; 'def': 0.14; 'output': 0.15; "'convert": 0.16; '--verbose': 0.16; 'commandline': 0.16; 'count,': 0.16; 'mine.': 0.16; 'skipped': 0.16; 'true:': 0.16; 'writer': 0.16; 'processor': 0.18; 'skip:{ 20': 0.18; 'usability': 0.18; 'input': 0.18; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'raise': 0.24; 'script': 0.25; 'header:User-Agent:1': 0.26; 'skip:# 10': 0.27; 'skip:e 30': 0.27; 'looks': 0.29; 'command-line': 0.29; 'prints': 0.29; '~ethan~': 0.29; 'convert': 0.29; "skip:' 10": 0.30; 'writes': 0.31; 'print': 0.31; 'skip:s 30': 0.31; '[1]': 0.32; 'scanned': 0.32; 'everyone': 0.34; 'running': 0.34; 'needed': 0.34; 'changed': 0.35; 'to:addr:python-list': 0.35; 'attempt': 0.35; 'list': 0.35; 'skip:{ 10': 0.36; 'received:10': 0.37; 'subject:: ': 0.37; 'skip:i 20': 0.37; "skip:' 20": 0.37; 'manual': 0.38; 'skip:v 20': 0.38; 'to:addr:python.org': 0.39; 'skip:t 20': 0.40; 'viewing': 0.61; 'user,': 0.67; 'opinions': 0.69; 'verification': 0.73; 'received:10.1.10': 0.84; 'subject:Using': 0.84; 'type=int,': 0.84; 'good,': 0.93 Date: Sun, 31 May 2015 22:08:06 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Using Python instead of Bash References: <87h9qsnckl.fsf@Equus.decebal.nl> In-Reply-To: <87h9qsnckl.fsf@Equus.decebal.nl> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433136649 news.xs4all.nl 2825 [2001:888:2000:d::a6]:38360 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91621 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. ;)