forked from 00nanhai/captchacker2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
characters_center.py
65 lines (55 loc) · 2.11 KB
/
characters_center.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
import os, sys, time
import cv,cv2
#import psyco
#psyco.full()
WIDTH = 20
WIDTH = 31
HEIGHT = 20
HEIGHT = 31
#folder_to_process = "DBTraining-Simulation_based"
#folder_to_process = "Egoshare/DBTest-Simulation_based"
#folder_to_process = "Hotmail\DBTraining"
def process_file(filenameIN, WIDTH = 31, HEIGHT = 31):
print "processing file: "+ filenameIN
if not (os.path.exists(filenameIN)):
print "file not found. Aborting."
return
else :
srcImg = cv.LoadImage(filenameIN,0)
res = cv.CreateImage( (WIDTH, HEIGHT), cv.IPL_DEPTH_8U, 1 )
cv.Set(res, 255)
xmin=WIDTH
xmax=0
ymin=HEIGHT
ymax=0
for i in range(srcImg.width):
for j in range(srcImg.height):
#print "xmax"
#print cv.Get2D(srcImg, j, i)
if cv.Get2D(srcImg, j, i)[0] == 0.0 :
#print "xin"
if i<xmin:
xmin = i
if i>xmax:
xmax = i
if j<ymin:
ymin=j
if j>ymax:
ymax=j
offsetx = (WIDTH - (xmax-xmin))/2
offsety = (HEIGHT - (ymax-ymin))/2
#print 'WIDTH',WIDTH,"offset",offsety,offsetx
for i in range(xmax-xmin):
for j in range(ymax-ymin):
if ((offsety+j>0) and (offsety+j<res.height) and (offsetx+i>0) and (offsetx+i<res.width)):
#print "haha"
cv.Set2D(res, offsety+j, offsetx+i, cv.Get2D(srcImg, ymin+j, xmin+i))
cv.SaveImage(filenameIN, res)
def process_folder(folder = "DBTraining-Simulation_based", WIDTH = 31, HEIGHT = 31):
print "Processing " + folder + " folder... "
if not (os.path.exists(folder ) ) :
print "Folder not found. Aborting."
return
for folder, subfolders, files in os.walk(folder):
for file in [file for file in files if 'bmp' in file]:
process_file(os.path.join(folder, file),WIDTH, HEIGHT)