├── .gitignore ├── LICENSE ├── README.md ├── Recognize.py ├── docs └── images │ ├── 0.jpg │ ├── 1.jpg │ ├── 1529.jpeg │ ├── 2.jpg │ ├── 3.jpg │ ├── 4.jpg │ ├── 5.jpg │ ├── 6.jpg │ ├── 7.jpg │ ├── 8.jpg │ └── 9.jpg └── zimo ├── 0.jpg ├── 1.jpg ├── 2.jpg ├── 3.jpg ├── 4.jpg ├── 5.jpg ├── 6.jpg ├── 7.jpg ├── 8.jpg └── 9.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | bin/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | eggs/ 16 | lib/ 17 | lib64/ 18 | parts/ 19 | sdist/ 20 | var/ 21 | *.egg-info/ 22 | .installed.cfg 23 | *.egg 24 | 25 | # Installer logs 26 | pip-log.txt 27 | pip-delete-this-directory.txt 28 | 29 | # Unit test / coverage reports 30 | htmlcov/ 31 | .tox/ 32 | .coverage 33 | .cache 34 | nosetests.xml 35 | coverage.xml 36 | 37 | # Translations 38 | *.mo 39 | 40 | # Mr Developer 41 | .mr.developer.cfg 42 | .project 43 | .pydevproject 44 | 45 | # Rope 46 | .ropeproject 47 | 48 | # Django stuff: 49 | *.log 50 | *.pot 51 | 52 | # Sphinx documentation 53 | docs/_build/ 54 | 55 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Halfcrazy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | DecodeValidateCode 2 | ================== 3 | 4 | An simple example in python to show how to decode validate code. 5 | 6 | # The basic case 7 | ![the simplest case](docs/images/1529.jpeg) 8 | 9 | As you seen it's a simplest case which contains four decimals. 10 | 11 | The solution can be described to three steps: *binary*, *devide*, *recognize*. 12 | ## 0)Prepare types 13 | This is a preparation step, so we called it step Zero. In this step we need to make all of possible occurs character to be a seperated image file. 14 | ![0](docs/images/0.jpg) 15 | ![1](docs/images/1.jpg) 16 | ![2](docs/images/2.jpg) 17 | ![3](docs/images/3.jpg) 18 | ![4](docs/images/4.jpg) 19 | ![5](docs/images/5.jpg) 20 | ![6](docs/images/6.jpg) 21 | ![7](docs/images/7.jpg) 22 | ![8](docs/images/8.jpg) 23 | ![9](docs/images/9.jpg) 24 | ## 1)Binary 25 | In this step we will transform the origin image into a black and white image. The theory is the color is consist of RGB three basic colors, so we can set a global threshold to do the binary operation. 26 | ## 2)Devide 27 | In this step we will split the image into four parts, each part contains a decimal. 28 | ## 3)Recognize 29 | In this step we wiil take each piece of our four parts to compare to the types we made in step zero and find the most similar one, and then we will know what character it is. After we done four parts we just need to join the four characters into one word and we got the anser. -------------------------------------------------------------------------------- /Recognize.py: -------------------------------------------------------------------------------- 1 | __author__ = 'halfcrazy' 2 | 3 | from PIL import Image 4 | import StringIO 5 | 6 | #------------------------------------------------------------------------------ 7 | # binaryzation 8 | 9 | 10 | def binary(data): 11 | data.seek(0) 12 | img = Image.open(data) 13 | pixdata = img.load() 14 | for y in xrange(img.size[1]): 15 | for x in xrange(img.size[0]): 16 | if pixdata[x, y][0] < 90: 17 | pixdata[x, y] = (0, 0, 0, 255) 18 | for y in xrange(img.size[1]): 19 | for x in xrange(img.size[0]): 20 | if pixdata[x, y][1] < 136: 21 | pixdata[x, y] = (0, 0, 0, 255) 22 | for y in xrange(img.size[1]): 23 | for x in xrange(img.size[0]): 24 | if pixdata[x, y][2] > 0: 25 | pixdata[x, y] = (255, 255, 255, 255) 26 | return img 27 | 28 | #------------------------------------------------------------------------------ 29 | # divide the image 30 | 31 | 32 | def division(img): 33 | font = [] 34 | for i in range(4): 35 | x = 6 + i * 13 36 | y = 3 37 | font.append(img.crop((x, y, x + 9, y + 13))) 38 | return font 39 | 40 | #------------------------------------------------------------------------------ 41 | # note that the image should be a StringIO object 42 | 43 | 44 | def recognize(img): 45 | fontMods = [] 46 | for i in range(10): 47 | fontMods.append((str(i), Image.open('./zimo/%d.jpg' % i))) 48 | result = '' 49 | font = division(img) 50 | for i in font: 51 | target = i 52 | points = [] 53 | for mod in fontMods: 54 | diffs = 0 55 | for yi in range(13): 56 | for xi in range(9): 57 | if mod[1].getpixel((xi, yi)) != target.getpixel((xi, yi)): 58 | diffs += 1 59 | points.append((diffs, mod[0])) 60 | points.sort() 61 | result += points[0][1] 62 | return result 63 | -------------------------------------------------------------------------------- /docs/images/0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/docs/images/0.jpg -------------------------------------------------------------------------------- /docs/images/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/docs/images/1.jpg -------------------------------------------------------------------------------- /docs/images/1529.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/docs/images/1529.jpeg -------------------------------------------------------------------------------- /docs/images/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/docs/images/2.jpg -------------------------------------------------------------------------------- /docs/images/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/docs/images/3.jpg -------------------------------------------------------------------------------- /docs/images/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/docs/images/4.jpg -------------------------------------------------------------------------------- /docs/images/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/docs/images/5.jpg -------------------------------------------------------------------------------- /docs/images/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/docs/images/6.jpg -------------------------------------------------------------------------------- /docs/images/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/docs/images/7.jpg -------------------------------------------------------------------------------- /docs/images/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/docs/images/8.jpg -------------------------------------------------------------------------------- /docs/images/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/docs/images/9.jpg -------------------------------------------------------------------------------- /zimo/0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/zimo/0.jpg -------------------------------------------------------------------------------- /zimo/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/zimo/1.jpg -------------------------------------------------------------------------------- /zimo/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/zimo/2.jpg -------------------------------------------------------------------------------- /zimo/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/zimo/3.jpg -------------------------------------------------------------------------------- /zimo/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/zimo/4.jpg -------------------------------------------------------------------------------- /zimo/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/zimo/5.jpg -------------------------------------------------------------------------------- /zimo/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/zimo/6.jpg -------------------------------------------------------------------------------- /zimo/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/zimo/7.jpg -------------------------------------------------------------------------------- /zimo/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/zimo/8.jpg -------------------------------------------------------------------------------- /zimo/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/halfcrazy/DecodeValidateCode/4cc5497186dbdbc105cfdfd000b32edd4a6c6237/zimo/9.jpg --------------------------------------------------------------------------------