Using Python instead of Bash

Cecil Westerhof Cecil at decebal.nl
Sun May 31 12:58:00 EDT 2015


Op Sunday 31 May 2015 16:14 CEST schreef Cem Karan:

>> 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.
>>
>> Normally I would implement this in Bash, but I thought it a good
>> idea to implement it in Python. This is my first try: 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()
>>
>> There have to be some improvements: display before printing,
>> possibility to change threshold, … But is this a good start, or
>> should I do it differently?
>
>
> As a first try, I think its pretty good, but to really answer your
> question, I think we could use a little more information.
>
> - Are you using python 2, or python 3? There are slightly easier
>   ways to do this using concurrent.futures objects, but they are
>   only available under python 3. (See
>   https://docs.python.org/3/library/concurrent.futures.html)

In principal I am using Python 3, but I prefer it to work also under
Python 2.

concurrent.futures is definitely something to look at, but not in this
case I think. First of all the processing does not take much time.
Secondly the order of the printing is important. Lastly, it is not
done now, but the conversion should be checked and maybe done over
with another threshold.


> - In either case, subprocess.call(), subprocess.check_call(), or
>   subprocess.check_output() may be easier to use.

I am using subprocess.check_call now. Much better.


> The following codes does the conversion in parallel, and submits the
> jobs to the printer serially.

Not needed now, but I file it for later use. ;-)


> def _convert(base_file_name): """ This code is slightly different
> from your code. Instead of using numbers as names, I use the base
> name of file and append '.png' to it. You may need to adjust this to

The reason I use numbers is that what I am printing is a booklet. The
input is IMG_4769.JPG and then I find 01.png much better, because then
I know which page it is. :-D


Thanks for the quit interesting read.

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



More information about the Python-list mailing list