Python enables automatic seek for ghost games, while python allows for seek
Http://blog.csdn.net/pipisorry/article/details/46564967
Find the ghost game Address [for beauty experts]
Game window Exploration
Download and install the PyWin32 Library (Python encapsulation for windows interfaces) program All Files "to find the required version.
Use spy ++ to locate the window handle (or find the window class name "lpClassName" and window name "lpWindowName") [specify the window-spy ++ usage in python]
Hwnd = win32gui. FindWindow ("MozillaWindowClass", "full screen game-Mozilla Firefox") if not hwnd: print (RED, 'window not found! ', DEFAULT) else: print (hwnd) # hwnd = 918912 # or directly use the handle
Note:
1.FindWindow function:
The FindWindow function retrieves and processes the class name and window name of the top-level window matching the specified string. This function does not search for subwindows.
Function: This function obtains the handle of a top-level window. The class name and window name of the window match the given string. This function does not search for subwindows. The search result is case insensitive.
Function Type: HWND FindWindow (LPCTSTR IpClassName, LPCTSTR IpWindowName );
Parameters:
IpClassName: pointer to an empty ending string with a specified class name or a member that identifies the class name string. If this parameter is a member, it must be a global member generated by the previous call to the theGlobafAddAtom function. This member has 16 bits. It must be at the lowest 16 bits of IpClassName, and the highest bits must be 0.
IpWindowName: point to an empty ending string that specifies the window name (window title. If this parameter is null, all windows are matched.
Return Value: If the function succeeds, the return value is a window handle with the specified class name and window name. If the function fails, the return value is NULL.
[FindWindow usage]
Findwindow-baike.baidu
2.Handle
Possible problems with using a handle directly:
1) In firefox, each page tab is the same handle, and the handle found in the program corresponds to the current tab. If the current tab is not used for seeking the game, it is incorrect.
2) When you use the window class name "lpClassName" and window name "lpWindowName", if the current tab is not found, the corresponding handle cannot be found.
An effective solution to the above problem is to open the seeking game in a new window separately.
You can also use spy ++ to find the corresponding handle window from the handle to check whether it is correct.
Game Image Extraction
The screenshot is used to extract the image. After finding the window, the screenshot is taken before the window is reached.
Download and install the PIL graphics processing Library [install python extension package in linux and windows-... PIL, pythonqt...]
Win32gui. showWindow (hwnd, win32con. SW_MAXIMIZE) # After the interface is forcibly displayed, the initial SW_RESTORE size is win32gui. setForegroundWindow (hwnd) # crop the window before it is mentioned # Get the full graph game_rect = win32gui. getWindowRect (hwnd) # src_image = ImageGrab. grab (game_rect) src_image = ImageGrab. grab (game_rect [0] + 20, game_rect [1] + 176, game_rect [2]-523, game_rect [1] + 176 + 514) # src_image.show () width, hight = src_image.size # crop left and right pictures respectively. left_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)
# Returns the left, top, right, and bottom borders of the image.
2.ImageGrab. grab Function: ImageGrab is a module of PIL for image capturing. ImageGrab without parameters. grab () takes a full screen screenshot and returns an Image object. You can also use a tuple as a parameter to specify the range to be captured (coordinates of the upper left and lower right points ), the two screenshots do not contain the mouse pointer.
The coordinates used above are all provided in order to demonstrate the simple code. In fact, variable parameters can be used and resolutions must be distinguished. The parameter can also be the left, top, right, and bottom boundary of the image to be captured.
Another ImageGrab. grabclipboard () allows you to collect images from the system clipboard.
3. Obtain the Image and then use it.Show () methodAnd use the default image viewing tool to enable debugging.
You can also use save (filename) to save it as a file, which can be obtained through Image. open (filename.
4.Crop (box) Method: Coordinates of the left and right punctuation
After grab obtains an Image object containing the left and right images, it uses the crop (box) method to crop the specified area and obtain the Left and Right game images.
Compare and obtain different regions of the two images.
Crop the two images into N small images for comparison, and the small images corresponding to the left and right unified regions are not equal to the "fault" area. The question is how to determine the inconsistency between the two images?
Image. histogram () function:Used to obtain the color histogram of the image. A Histogram can represent the number of different brightness (or color) values in an image. The histograms of two natural images are basically different, unless the two images are symmetric, consistent colors, but arranged differently, after the two graphs are further divided, the histograms of the subgraphs are also different. A histogram is a conversion from a graph to a numeric value. Comparing the color values of the two graphs, we can see whether there is any difference. For an image in RBG color format, the histogram () function returns an array of 768 characters. The value 0-255 indicates the red 0-255, and the value 0-indicates the green color of the-table, 255-in blue,Numeric value indicates the number of pixels in the color.. Therefore, the sum of all the Members in the histogram () List is equal to the pixel value x 3 of the modified image.
This function is used to obtain the numerical difference between the two charts.
Def channel_compare (cha_a, cha_ B): ''' compares the differences between the two color channels and returns ''' sum_a = sum ([I * v for I, v in enumerate (cha_a)]) sum_ B = sum ([I * v for I, v in enumerate (cha_ B)]) # red_a = 0 # red_ B = 0 # for I in range (0,256 ): # red_a + = histogram_a [I + 0] * I # red_ B + = histogram_ B [I + 0] * I if sum_a + sum_ B> 0: diff_channel = abs (sum_a-sum_ B) * 10000/max (sum_a, sum_ B) return diff_channeldef image_compare (ima Ge_a, image_ B): ''' returns the difference value between the two graphs, and returns the ten-thousandth difference between the two graphs in red, green, and blue, and '''histogram_a = image_a.histogram () histogram_ B = image_ B .histogram () if len (histogram_a )! = 768 or len (histogram_ B )! = 768: print (RED, "get histogram error", DEFAULT) return None print ([I * v for I, v in enumerate (histogram_a)] []) diff_red = channel_compare (histogram_a [: 256], histogram_ B [: 256]) diff_green = channel_compare (histogram_a [256: 512], histogram_ B [256: 512]) diff_blue = channel_compare (histogram_a [512: 768], histogram_ B [512: 768]) return diff_red, diff_green, diff_blue
Note: Adds the red, green, and blue Difference values returned by the function. If the value exceeds the predefined threshold of 2000, the region is different. This calculation method is a bit "earthy", but it is very effective for the problem to be solved this time and will not continue to improve.
Crop the two images into N small images for comparison.
# Crop the left and right big charts into multiple small graphs for comparison. result = zeros (width // 10, hight // 10) for col in range (0, width // 10): for row in range (0, hight // 10): clip_box = (col * 10, row * 10, (col + 1) * 10, (row + 1) * 10) rows = upper (clip_box) clip_image_right = lower (clip_box) clip_diff = image_compare (lower, clip_image_right) if sum (clip_diff)> 2000: result [row] [col] = 1
Note: The big chart is 780*520, separated into 10x10 small pieces, defines a 78*52 two-digit Array Storage results, respectively, after comparison, the difference value greater than the threshold array area marked as 1.
Mark different regions on both sides of the game
You can use the PyWin32 function to draw the game window handle directly after obtaining it. However, you must be familiar with Windows programming to solve the problem of erasing my mark after the game redraws itself.
You can also use Qt. The following uses Qt to create a QWidget window that is as transparent as the game size. It is superimposed on the game window and marked with a mask. The marked data has been recorded in the result array. Drawing a square at the specified position indicates that the area is different between the left and right sides. Note that the boundary between the two squares should not be drawn to avoid interference with the game. In addition to marking, two buttons are drawn to trigger comparison and erasure.
I didn't replace the variable here. It's too troublesome. Just look at the algorithm clearly.
From: http://blog.csdn.net/pipisorry/article/details/46564967
Ref: PIL Document