-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanify.py
executable file
·33 lines (26 loc) · 1.11 KB
/
scanify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
import os
import argparse
def get_parser():
"""Get parser of arguments"""
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-g', '--grayscale', action='store_true')
parser.add_argument('file')#, nargs='+')
return parser
def scanify(fn, grayscale=False):
"""Scanify a file.
Credits http://tex.stackexchange.com/questions/94523/simulate-a-scanned-paper
http://www.imagemagick.org/Usage/photos/#color-in
.. warning :: required imagemagick
"""
nfn = os.path.splitext(fn)[0] + '_scan.png'
grayscale_cmd = ' -colorspace gray ' if grayscale else ''
os.system('convert "' + fn + '" ' + grayscale_cmd + ' \( +clone -blur 0x1 \) +swap -compose divide -composite -linear-stretch 5%x0% "' + nfn + '"')
print('%s save' % nfn)
from IPython.display import Image
return Image(filename=nfn)
if __name__ == '__main__':
parser = get_parser()
args = parser.parse_args()
scanify(args.file, args.grayscale)