Using Python instead of Bash

Cecil Westerhof Cecil at decebal.nl
Sun May 31 12:36:47 EDT 2015


Op Sunday 31 May 2015 16:02 CEST schreef Alain Ketterlin:

> Cecil Westerhof <Cecil at decebal.nl> writes:
>
>> I help someone that has problems reading. For this I take photo's
>> of text, use convert from ImageMagick to make a good contrast
>> (original paper is grey) and use lpr to print it a little bigger.
>
>> import glob
>> import subprocess
>>
>> treshold = 66 count = 0 for input in sorted(glob.glob('*.JPG')):
>> count += 1 output = '{0:02d}.png'.format(count) print('Going to
>> convert {0} to {1}'.format(input, output)) p =
>> subprocess.Popen(['convert', '-threshold', '{0}%'.format(treshold),
>> input, output]) p.wait() print('Going to print {0}'.format(output))
>> p = subprocess.Popen(['lpr', '-o', 'fit-to-page', '-o', 'media=A4',
>> output]) p.wait()
>
> Maybe using check_call() would be simpler, but it will, well, check
> for the exit code of convert/lpr (which you should do anyway).

That is a lot better yes. I did not like that I needed two calls.


> And I would call that variable "threshold" instead of "treshold".

Yep, I saw the mistake after posting. :-(


> (What I don't see is the advantage you find in writing such scripts
> in python instead of sh, but I guess you have your own reasons.)

Several.

The first is that I want to get some Python experience.

But later on I want to display the converted photo and if it is not
correct, change the threshold and convert it again with a new
threshold. 66% is good most of the time, but not always. Sometimes it
has to be bigger or smaller.

And after that I want to put a GUI around it.

So, that is why I am using Python.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list