Skip to content

Instantly share code, notes, and snippets.

@dafeiroc
Created October 6, 2015 11:35
Show Gist options
  • Save dafeiroc/e1c0a647cbef3f05d78f to your computer and use it in GitHub Desktop.
Save dafeiroc/e1c0a647cbef3f05d78f to your computer and use it in GitHub Desktop.
What does it do here ?
split_position = []
for i in xrange(1, img.size[0]):
if black_pix[i] != 0 and black_pix[i - 1] == 0:
if len(split_position) % 2 == 0:
split_position.append(i)
elif black_pix[i] == 0 and black_pix[i - 1] != 0:
if i - 1 - split_position[-1] >= 6:
split_position.append(i - 1)
if split_position[1] > 17:
insert_index(1, 10, 16, black_pix, split_position)
if split_position[3] > 27:
insert_index(3, 20, 26, black_pix, split_position)
if split_position[5] > 37:
insert_index(5, 30, 36, black_pix, split_position)
if split_position[7] > 47:
insert_index(7, 40, 46, black_pix, split_position)
if len(split_position) != 8:
return "alreadfail"
region = img.crop((split_position[0], 0, split_position[1] + 1, img.size[1]))
region.save("%s%s%02d.png" % (target_dir, f[0 : -4], 1))
region = img.crop((split_position[2], 0, split_position[3] + 1, img.size[1]))
region.save("%s%s%02d.png" % (target_dir, f[0 : -4], 2))
region = img.crop((split_position[4], 0, split_position[5] + 1, img.size[1]))
region.save("%s%s%02d.png" % (target_dir, f[0 : -4], 3))
region = img.crop((split_position[6], 0, split_position[7] + 1, img.size[1]))
region.save("%s%s%02d.png" % (target_dir, f[0 : -4], 4))
@zeqing-guo
Copy link

我在代码上加了注释,你可以看一下。我觉得你的验证码似乎更加容易切一点,因为两个字符之间分得很开,而且也没有什么明显的扭曲,你可以直接估算一下每个字符出现和结束的位置,然后切割。也可以像我这里一下写一个循环,根据前一个像素点是白色的后一个像素点是黑色的判定字符出现;根据前一个像素点是黑色的后一个像素点是白色的判定字符借宿,然后根据判定的位置进行切割。

果如还有哪儿不懂的话再问我吧~

      split_position = []
      // 判断字符左右两边的横座标这里和之后出现的字符的意思就是验证码图片里出现的单个数字或者字母
      for i in xrange(1, img.size[0]):
            // 前一个像素点不是黑色的后一个像素点是黑色的说明出现了一个字符
            if black_pix[i] != 0 and black_pix[i - 1] == 0:
                // 这个if是一个针对我破解的验证码的优化和算法整体的思想无关你可以把if去掉去理解
                if len(split_position) % 2 == 0:
                    split_position.append(i)
            // 前一个像素点是黑色的后一个不是黑色的说明字符结束了
            elif black_pix[i] == 0 and black_pix[i - 1] != 0:
                // 这同样是针对我破解的验证码的优化没有一般性你的验证码如果没出现前后两个字符连在一起的情况的话可以忽略它
                if i - 1 - split_position[-1] >= 6:
                    split_position.append(i - 1)

        // 针对复旦验证码的优化优化原因是复旦的验证码两个字符经常连在一起不好切割你可以忽略这部分
        if split_position[1] > 17:
            insert_index(1, 10, 16, black_pix, split_position)
        if split_position[3] > 27:
            insert_index(3, 20, 26, black_pix, split_position)
        if split_position[5] > 37:
            insert_index(5, 30, 36, black_pix, split_position)
        if split_position[7] > 47:
            insert_index(7, 40, 46, black_pix, split_position)
        if len(split_position) != 8:
            return "alreadfail"

        // 根据上面找到的字符开始和结束的位置把图片切割开来
        region = img.crop((split_position[0], 0, split_position[1] + 1, img.size[1]))
        region.save("%s%s%02d.png" % (target_dir, f[0 : -4], 1))
        region = img.crop((split_position[2], 0, split_position[3] + 1, img.size[1]))
        region.save("%s%s%02d.png" % (target_dir, f[0 : -4], 2))
        region = img.crop((split_position[4], 0, split_position[5] + 1, img.size[1]))
        region.save("%s%s%02d.png" % (target_dir, f[0 : -4], 3))
        region = img.crop((split_position[6], 0, split_position[7] + 1, img.size[1]))
        region.save("%s%s%02d.png" % (target_dir, f[0 : -4], 4))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment