-
Notifications
You must be signed in to change notification settings - Fork 0
/
procesScan.py
94 lines (78 loc) · 2.54 KB
/
procesScan.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import codecs
import sys
import os
import glob
import time
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
from skimage import data, img_as_float
from skimage import exposure
from skimage import io
from skimage import transform
scale = 0.75
# function for escape []
def escapeBraceForGlob(str):
# convert [ -> [[] , ] -> []]
newStr = str.replace("[","\\[").replace("]","\\]")
newStr = newStr.replace("\\[","[[]").replace("\\]","[]]")
return newStr
# script for optimize contrast of scanned book
sys.stdin = codecs.getreader('utf-8')(sys.stdin)
path = sys.argv
rawpath = path[1]
path = escapeBraceForGlob(rawpath)
print "run at: " + path
if os.path.exists(rawpath[0:-1]+"_re/")==True:
print "directory exist"
else:
os.mkdir(rawpath[0:-1]+"_re/")
# Image processing
i = 1
t = time.time()
for file in glob.glob(path+'/*.jpg'):
print(file)
savefilename = "re_"+"{0:04d}".format(i)+".jpg"
print "=>: " + savefilename
# image loading & processing
img = io.imread(file, 0)
# plt.hist(img.ravel(), 128)
# plt.show()
# discriminate color page
if img.shape[2]==3:
# imzR = stats.mstats.zscore(img[0:,0:,0].ravel())
# imzG = stats.mstats.zscore(img[0:,0:,1].ravel())
# imzB = stats.mstats.zscore(img[0:,0:,2].ravel())
# buf, p1 = stats.wilcoxon(imzR,imzG)
# buf, p2 = stats.wilcoxon(imzG,imzB)
# buf, p3 = stats.wilcoxon(imzB,imzR)
# r = np.corrcoef(imzR,imzB)
# r2 = np.corrcoef(imzR,imzG)
grayim = True
else:
grayim = True
# print np.median(imzR) #np.median(img[0:,0:,0].ravel())
# print np.median(imzG) #np.median(img[0:,0:,1].ravel())
# print np.median(imzB) #np.median(img[0:,0:,2].ravel())
# print r,r2
# if (p1 < 0.05) | (p2 < 0.05) | (p3 < 0.05):
# grayim = False
# print "Color!"
# else:
# grayim = True
# img = np.mean(img, 2)
# print "Gray!"
p2, p98 = np.percentile(img, (1, 85))
img_rescale = exposure.rescale_intensity(img, in_range=(p2, p98))
scaleShape = scale*np.array(img_rescale.shape)
scaleShape = scaleShape.astype(np.int32)
scaleShape[2] = 3;
img_rescale = transform.resize(img_rescale, scaleShape)
io.imsave(rawpath[0:-1]+"_re/"+savefilename, img_rescale)
i = i+1
print "======Complete Batch ====== "
# Report time elapsed
elapsed = time.time() - t
print("{0:04f}".format(elapsed)+"[sec]/ "+"{0:04d}".format(i)+" [frames]")