Created
December 3, 2018 18:33
-
-
Save Pixelbo/1f23f3fc16ec67654a7a56f71348adc3 to your computer and use it in GitHub Desktop.
save
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pyscreenshot as ImageGrab | |
| import cv2 | |
| import numpy as np | |
| import time | |
| numbers = [r"D:\2048-2.png", r"D:\2048-4.png", r"D:\2048-8.png", r"D:\2048-16.png"] | |
| colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 0, 255)] | |
| x = [] | |
| y = [] | |
| centerx = [] | |
| centery = [] | |
| name = [] | |
| names = [] | |
| test = [] | |
| liney = [] | |
| liney2 = [] | |
| linex = [] | |
| verticalmatch = [] | |
| ##main sript## | |
| if __name__ == "__main__": | |
| #prends un screenshot et le sauvgarde | |
| im=ImageGrab.grab() | |
| im.save(r'D:\2048bot.png') | |
| #matching templates avec rectangles | |
| global color | |
| color = 0 | |
| for img in numbers: | |
| img_rgb = cv2.imread(r'D:\2048bot.png') | |
| gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) | |
| template = cv2.imread(img ,0) | |
| res = cv2.matchTemplate(gray, template, cv2.TM_SQDIFF_NORMED) | |
| min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) | |
| min_thresh = (min_val + 1e-6) * 1.5 | |
| match_locations = np.where(res <= min_thresh) | |
| w, h = template.shape[::-1] | |
| for (xpt, ypt) in zip(match_locations[1], match_locations[0]): | |
| x.append(xpt) | |
| y.append(ypt) | |
| name.append(numbers[color]) | |
| cv2.rectangle(img_rgb, (xpt, ypt), (xpt + w, ypt + h), colors[color], 1) | |
| test.append((xpt + w, ypt + h)) | |
| cv2.imwrite(r'D:\2048bot.png',img_rgb) | |
| color += 1 | |
| # triage des positions x et y n°2 et marque cercle au centre puis rajoute la valeur du centre dans une liste | |
| yyyy = 0 | |
| for xxxx in x: | |
| w2 = np.round(w / 2) | |
| h2 = np.round(h / 2) | |
| img = cv2.circle(img_rgb, (xxxx + int(w2), y[yyyy] + int(h2)), 5, (255, 0, 255), -1) | |
| centerx.append(xxxx + int(w2)) | |
| centery.append(x[yyyy] + int(h2)) | |
| yyyy += 1 | |
| cv2.imwrite(r'D:\2048bot.png', img_rgb) | |
| # triage des positions des plaques | |
| j = 0 | |
| p=14 | |
| o = 0 | |
| time.sleep(1) | |
| for i in range(10000): | |
| try: | |
| names.append(name[j]) | |
| j += p | |
| p += o | |
| o += 2 | |
| except: | |
| pass | |
| for name1 in name: | |
| name2 = name.index(name1) | |
| if not name2 == len(name)-1: | |
| if name[name2] == name[name2+1]: | |
| x1 = x[name2] | |
| y1 = y[name2] | |
| x2 = x[name2+1] | |
| y2 = y[name2+1] | |
| cv2.line(img_rgb, (x1 + int(w2), y1 + int(h2)), (x2 + int(w2), y2 + int(h2)), 5) | |
| cv2.imwrite(r'D:\2048bot.png', img_rgb) | |
| liney.append(y1 + int(h2)) | |
| liney.append(y2 + int(h2)) | |
| linex.append(x1 + int(w2)) | |
| linex.append(x2 + int(w2)) | |
| name[name2] = name1 + "go" | |
| else: | |
| pass | |
| liney2 = liney | |
| for lines in liney2: | |
| lines2 = liney2.index(lines) | |
| if not lines2 == len(liney2)-1: | |
| if liney2[lines2] - liney2[lines2+1] == -109 and linex[lines2] == linex[lines2+1]: | |
| print("yeah") | |
| verticalmatch.append((liney2[lines2], liney2[lines2 + 1])) | |
| cv2.rectangle(img_rgb, verticalmatch, ) | |
| liney2[lines2] = lines + 1000 | |
| else: | |
| print("nope") | |
| liney2[lines2] = lines + 1000 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment