Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
5.1 kB
1
Indexable
Never
from os import path
from google_images_search import GoogleImagesSearch
import numpy as np
from io import BytesIO
from PIL import Image
from ahk import AHK
import keyboard
import autoit as au



ahk = AHK()
th = 100



my_bytes_io = BytesIO()
coords = []
sizeXY = 250
spd = 1
pixels = [[0]*sizeXY for i in range(sizeXY)]
search = "tiger"
gis = GoogleImagesSearch('AIzaSyDyLcJgLo5Ow3BKGVMmdJZNk0_SJMbDtms', '349654228cd8b4f43')
searchPrams = {
 'q': search,
 'num': 1,
 'imgType': 'clipart',   
}
gis.search(search_params=searchPrams)

for image in gis.results():
    # here we tell the BytesIO object to go back to address 0
    my_bytes_io.seek(0)

    # take raw image data
    raw_image_data = image.get_raw_data()

    # this function writes the raw image data to the object
    image.copy_to(my_bytes_io, raw_image_data)

    # or without the raw data which will be automatically taken
    # inside the copy_to() method
    image.copy_to(my_bytes_io)

    # we go back to address 0 again so PIL can read it from start to finish
    my_bytes_io.seek(0)

    # create a temporary image object
    img = Image.open(my_bytes_io).convert('RGB')
    img = img.resize((sizeXY,sizeXY),Image.ANTIALIAS)
    pix_val = list(img.getdata())
    img.show()


for x in range(0,sizeXY):
    for y in range(0,sizeXY):
        r,g,b = img.getpixel((x,y))
        s = (r + g + b) / 3
        if s > 115:
            pixels[x][y] = 0
        else:
            pixels[x][y] = 1


def draw():
    cntr = au.mouse_get_pos()
    x0 = cntr[0] - (sizeXY/2)
    y0 = cntr[1] - (sizeXY/2)

    ahk.mouse_move(x0,y0)
    ahk.mouse_move(x0+sizeXY,y0)
    ahk.mouse_move(x0+sizeXY,y0+sizeXY)
    ahk.mouse_move(x0,y0+sizeXY)
    ahk.mouse_move(x0,y0)

    stk = makeStack(1000)
    for x in range(0,sizeXY):
        for y in range(0,sizeXY):
            if pixels[x][y] == 1:
                ahk.mouse_move(x+x0,y+y0)
                au.mouse_down()
                drawArea(stk, x, y, x0, y0)
                au.mouse_up()

    for x in range(0,sizeXY):
        for y in range(0,sizeXY):
            if (pixels[x][y] == 2):
                pixels[x][y] = 1     

def drawArea(stk, x, y, x0, y0):
    while True:
        if keyboard.is_pressed('q'): quit()
        ahk.mouse_move(x + x0, y + y0)
        pixels[x][y] = 2

        cont = False
        for i in (3,6,1,8,4,5,2,7):
            if keyboard.is_pressed('q'): quit()    
            if i == 1:
                if ((x > 0) and (y > 0)):
                    if (pixels[x-1][y-1]==1):
                        np.append(stk,(x,y))
                        x -= 1
                        y -= 1
                        cont = True
            elif i == 2:
                if (y > 0):
                    if (pixels[x][y-1]==1):
                        np.append(stk,(x,y))
                        y -= 1
                        cont = True
            elif i == 3:    
                if (x > 0) and (y < 0):
                    if (pixels[x+1][y-1]==1):
                        np.append(stk,(x,y))
                        x += 1
                        y -= 1
                        cont = True
            elif i == 4:
                if (x > 0):
                    if (pixels[x-1][y] == 1):
                        np.append(stk,(x,y))
                        x -= 1
                        cont = True
            elif i == 5:            
                if (x < (sizeXY - 1)):
                    if (pixels[x+1][y] == 1):
                        np.append(stk,(x,y))
                        x += 1
                        cont = True
            elif i == 6:
                if ((x < 0) and (y > 0)):
                    if (pixels[x-1][y+1] == 1):
                        np.append(stk,(x,y))
                        x -= 1
                        y += 1
                        cont = True
            elif i == 7:
                if (y < (sizeXY - 1)):
                    if (pixels[x][y+1] == 1):
                        np.append(stk,(x,y))
                        y += 1
                        cont = True
            elif i == 8:                        
                if ((x < (sizeXY - 1)) and (y < sizeXY - 1)):
                    if (pixels[x+1][y+1] == 1):
                        np.append(stk,(x,y))
                        x += 1
                        y += 1
                        cont = True

        if (cont): continue
        c, x1, y1 = pop(stk,x,y)
        x = x1
        y = y1
        if (not c):
            x = x1
            y = y1
            break
        
            
def makeStack(s):
    stack = np.empty(((s+1),2))
    stack[0][0] = 0
    stack[0][1] = s
    return stack

def pop(stack, x, y):
    if (stack[0][0] < 1):
        return False, x, y
    
    x = stack[(stack[0][0])][0]
    y = stack[(stack[0][0])][1]

    stack[0][0] -= 1
    return True, x, y

keyboard.wait('f')
draw()