Python realizes auto-picking game

Source: Internet
Author: User

http://blog.csdn.net/pipisorry/article/details/46564967

Pick-up game address [beauty everyone to finding fault]

Game window Sniffing

Download install PyWin32 library (Python package for Windows interface) http://sourceforge.net/projects/pywin32/, but not directly point download icon, Otherwise down is a Readme.txt, click "Browse all Files" to find the desired version.

Use Spy + + to find the window handle (or find the window class name lpclassname and window name lpwindowname) [python Specifies the window-spy++ usage ]

hwnd = Win32gui. FindWindow ("Mozillawindowclass","Game fullscreen-Mozilla Firefox")if nothwnd:    Print(RED,' window not found! ', DEFAULT)Else:    Print(hwnd)# hwnd = 918912 #或者直接使用句柄
Note:

1. FindWindow function:

FindWindow This function retrieves the class name that handles the top-level window and the window name matches the specified string. This function does not search child windows.

function function: This function obtains a handle to a top-level window that matches the given string with the class name and window name of the window. This function does not find child windows. is not case-sensitive when searching.
Function type: HWND FindWindow (LPCTSTR ipclassname,lpctstr ipwindowname);
Parameters:
Ipclassname: A pointer to an empty end string that specifies the class name, or a member that identifies the class name string. If the parameter is a member, it must be the global member produced by the previous call to the Theglobafaddatom function. The member is 16 bits and must be in the low 16 bits of the ipclassname and the high must be 0.
Ipwindowname: Points to an empty end string that specifies the window name (window caption). If the argument is empty, all Windows match.
Return value: If the function succeeds, the return value is the window handle with the specified class name and window name, and if the function fails, the return value is null.

[FindWindow usage]

[FindWindow-baike.baidu]

2. Handle

Possible problems with using the handle directly:

1) each page tab in Firefox is the same handle, the handle found in the program corresponds to the current tab, if the current tab is not a fault of the game is wrong

2) Use the window class name lpclassname and window name Lpwindowname If the current tab is not a pick-up game will not find the corresponding handle

An effective solution to the problem above is to open the puzzle game separately in a new window

You can also use the Spy + + reverse clause to find the corresponding window of the handle and check if it is correct.


Game Picture Extraction

Extract the image using a screenshot of the way, find the window will refer to the front window, and then a window screenshot.

Download and install the PIL graphics processing library [Linux and Windows install Python expansion pack-... PIL, PYTHONQT...]

Win32gui. ShowWindow (hwnd, win32con.sw_maximize)# Sw_restore The initial size after forcing the interface to be displayed .Win32gui. SetForegroundWindow (HWND)# will the window refer to the front# Crop to get full viewGame_rect= Win32gui. GetWindowRect (HWND)# src_image = Imagegrab.grab (game_rect)Src_image= Imagegrab.grab ((game_rect[0]+  -, game_rect[1]+ 176, game_rect[2]- 523, game_rect[1]+ 176+ 514))# src_image.show ()width, hight= Src_image.size# Cut the left and right content pictures separatelyLeft_box= (0,0, width// 2+ 1, hight) Right_box= (width// 2+ 1,0, width, hight) image_left= Src_image.crop (Left_box) image_right= Src_image.crop (Right_box)# image_left.show ()# image_right.show ()
Note:

1.Win32gui. GetWindowRect function

l,t,r,b =win32gui.GetWindowRect(self.hwnd)#返回图形左, top, right, and bottom borders

2.imagegrab.grab function : Imagegrab is a module of PIL for image fetching. A full screen screenshot of Imagegrab.grab () without parameters, return an Image object, or use a tuple as a parameter to specify the range to intercept (the coordinates of the top-left and bottom-right two points), both of which are not with the mouse pointer.

The coordinates used above are simply filled out to demonstrate the code, but you can actually use variable parameters and distinguish between resolutions. Parameters can also be the left, top, right, and bottom boundaries of the graph to intercept

There is also a imagegrab.grabclipboard () to capture images from the system Clipboard.

3. You can use the Show () method When you get image images and open them using the system default image viewing tool for easy debugging.

It can also be saved as a file with Save (filename), which can be image.open (filename) Open.

4. Crop (Box) method : The parameter is upper left and right lower punctuation coordinates

Grab get an Image object containing left and right images, the crop (box) method can be used to crop the area specified in it, respectively, to get about two game pictures.


Compare to get two different areas of the map content

The two graph is cropped to n small pictures, respectively, the uniform region corresponding to the small picture is not equal to the "stubble" area, the question is how to determine the content of two images inconsistent?

Image.histogram () function: The color histogram used to get the image. The histogram can represent the number of brightness (or color) in a picture, the histogram of two natural pictures is basically different, unless the two is symmetrical, the color is consistent but the arrangement is different, but even so, the two graph continues to divide, the histogram of its sub-graph will be different. A histogram is a graph-to-value conversion, comparing the color values of two graphs to see if there is a difference. An image in RBG color format, the histogram () function returns an array of length 768, 第0-255 represents the red 0-255, the 第256-511 table color is green 0-255, 第512-767 the table color blue 0-255, The numeric value represents the number of color pixels . Therefore, the sum of all members of the histogram () list is equal to the pixel value of the image modified by x 3.

A function to obtain a numerical difference of two graph comparison

defChannel_compare(cha_a,Cha_b):    " "compares the difference values of two color channels and returns" "    Sum_a= sum([I* V forI, VinchEnumerate(cha_a)]) Sum_b= sum([I* V forI, VinchEnumerate(Cha_b)])# red_a = 0# red_b = 0# for I in range (0,.):# Red_a + = histogram_a[i + 0] * I# Red_b + = histogram_b[i + 0] * I    offSum_a+ Sum_b>0:        Diff_channel= ABS(Sum_a- Sum_b)* 10000/ Max(Sum_a, Sum_b)returnDiff_channeldefImage_compare(image_a,Image_b):    " "returns the difference value of the two graph and returns the sum of the two figures of the red-green-blue difference" "    Histogram_a= image_a. Histogram () Histogram_b= Image_b. Histogram ()ifLen(histogram_a)!= 768orLen(Histogram_b)!= 768:        Print(RED,"Get histogram error", DEFAULT)return None    Print([I* V forI, VinchEnumerate(Histogram_a)] [0:Ten]) diff_red= Channel_compare (histogram_a[: the], histogram_b[: the]) Diff_green= Channel_compare (histogram_a[ the: +], histogram_b[ the: +]) Diff_blue= Channel_compare (histogram_a[ +:768], histogram_b[ +:768])returnDiff_red, Diff_green, Diff_blue
Note: The value of the red-green-blue difference returned by the function is added, and if the predetermined threshold value of 2000 is exceeded, the range is different. This calculation is a bit "Earth", but the problem to be solved is very effective, no further improvement.

Cut the two graph into n small images and compare them to each other.

# Cut the left and right big picture into multiple small plots to compare each otherResult= Zeros ((Width// Ten, hight// Ten)) forColinchRange(0, width// Ten):     forRowinchRange(0, hight// Ten):        Clip_box= (col* Ten, row* Ten, (col+ 1)* Ten, (row+ 1)* Ten) Clip_image_left= Image_left.crop (Clip_box) clip_image_right= Image_right.crop (Clip_box) Clip_diff= Image_compare (Clip_image_left, Clip_image_right)ifsum(Clip_diff)> -:            Result[row][col]= 1
Note: The big picture is780*520, a small block separated into 10x10, defines a 78*52 two-bit array to store the results, respectively, and compares the array area with the difference greater than the threshold value to 1.



from:http://blog.csdn.net/pipisorry/article/details/46564967

Ref:pil Document


Python realizes auto-picking game

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.