python handles the graphic verification of Google and 12306 differently

Posted by jtron85 on Tue, 03 Dec 2019 17:03:19 +0100

With the development of anti crawler, there are not only slider verification, but also point selection verification, graphic verification, Chinese character graphic verification, among which Google's graphic verification and 12306's graphic verification are more well-known, as well as the inverted Chinese character verification.


In addition to docking the professional verification platform, we can also choose the click location by sending the pictures to ourselves via Email and wechat. Today, we will share how to facilitate our click selection.

Here is a 12306 graphic verification code


In this case, in large projects, the professional processing platform is directly connected, and in small projects, you can click it, but how to choose without interface mode? Of course, it is sent to the mobile phone via email or wechat. Even if it is verified, it is difficult to deal with it.


But make the above picture look like this:


Code directly in the wechat reply picture. The cross cursor in the middle of each number is the coordinate that will be clicked after selecting the number. At the same time, we can define the size of image segmentation, which can be more dense or more sparse.

Each cell is like a pixel position, but the difference is that we just need to say the number and let the program find the coordinates automatically to click.


The code is as follows:

from PIL import Image
from PIL import ImageDraw, ImageFont


def images(file, n, m):
    """

    :param file: Picture path
    :param n: x How many copies of shaft cutting
    :param m: y How many copies of shaft cutting
    :return: 
    """    img = Image.open(file)
    img_d = ImageDraw.Draw(img)
    x_len, y_len = img.size
    x_ = x_len // n
    y_ = y_len // m
    print(x_*y_)
    for x in range(0, x_len, x_):
        img_d.line(((x, 0), (x, y_len)), (00180))
    for y in range(0, y_len, y_):
        img_d.line(((0, y), (x_len, y)), (00180))
    fontsize = min(y_, x_)*3//4
    draw = ImageDraw.Draw(img)
    ttfont = ImageFont.truetype('simsunb.ttf', fontsize)
    data = dict()    # Dictionary of each number and its corresponding coordinates
    i = 1
    while i <= n*m:
        for yy in range(y_//2, y_len, y_):
            for xx in range(x_//2, x_len, x_):
                data[i] = (xx, yy)
                i += 1

    for key, value in data.items():
        xx, yy = value
        draw.text((xx-fontsize*1.5/4, yy-fontsize*0.5), str(key), fill=(25500),
                  font=ttfont)
        img_d.line(((xx-2, yy), (xx+2, yy)), (00180))
        img_d.line(((xx, yy-2), (xx, yy+2)), (00180))

    img.show()
    img.save('ii.png')


if __name__ == '__main__':
    images('1.png'1410)    #


Of course, the effect is as follows:


Topics: Python Google Mobile