Guest User

rgb_hsv

a guest
Jan 9th, 2018
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. from PIL import Image
  2. import pyscreenshot as ImageGrab
  3. from time import sleep
  4. import pyautogui
  5.  
  6. #--------------------------------------
  7. #please change
  8. nextBlock1 = [660,142]
  9. nextBlock2 = [660,210]
  10. nextBlock3 = [660,276]
  11.  
  12. currentBlock = [386,75]
  13.  
  14. holdBlock = [136,140]
  15.  
  16. #please change
  17. #User must change these varibles for specific screen size.
  18. #Current values are set for 1920x1080 screens
  19. x_pad = 556
  20. y_pad = 352
  21. x_width = 1348
  22. y_length = 944
  23.  
  24. #starting square for bottom left cell
  25. x_start = 279
  26. y_start = 542
  27. #-------------------------------------
  28.  
  29. gameMatrix = [[0 for x in range(10)] for y in range(20)]
  30.  
  31. def rgb2hsv(r, g, b):
  32.     #converts rgb to hsv
  33.     r, g, b = r/255.0, g/255.0, b/255.0
  34.     mx = max(r, g, b)
  35.     mn = min(r, g, b)
  36.     df = mx-mn
  37.     if mx == mn:
  38.         h = 0
  39.     elif mx == r:
  40.         h = (60 * ((g-b)/df) + 360) % 360
  41.     elif mx == g:
  42.         h = (60 * ((b-r)/df) + 120) % 360
  43.     elif mx == b:
  44.         h = (60 * ((r-g)/df) + 240) % 360
  45.  
  46.     if mx == 0:
  47.         s = 0
  48.     else:
  49.         s = df/mx
  50.    
  51.     return (int(h),int(((s*10)*10)))
  52.  
  53.  
  54. def getBlockType (rgb_value):
  55.     #enter in a rgb list and returns the colour of that block
  56.  
  57.     h,s = rgb2hsv(rgb_value[0],rgb_value[1],rgb_value[2])
  58.     print(s)
  59.  
  60.  
  61.     if (h >=285 and h <=305):
  62.         #purple colour
  63.         print("purple")
  64.         return(1)
  65.     elif (h >= 200 and h <=212):
  66.         #blue colour
  67.         print("blue")
  68.         return(2)
  69.     elif (h >= 180 and h <=190):
  70.         #light blue colour
  71.         print("light blue")
  72.         return(3)
  73.     elif (h >= 130 and h <=141):
  74.         #green colour
  75.         print("green")
  76.         return(4)
  77.     elif (h >= 50 and h <=60):
  78.         #yellow colour
  79.         print("yellow")
  80.         return(5)
  81.     elif (h >= 35 and h <=45):
  82.         #orange colour
  83.         print("orange")
  84.         return(6)
  85.     elif (h >= 0 and h <=2 and s >= 20):
  86.         #red colour
  87.         print("red")
  88.         return(7)
  89.     elif (h == 0 and s == 0):
  90.         print("Invalid Cords")
  91.     else:
  92.         print("")
  93.         return(0)
  94.  
  95. def getNextBlocks ():
  96.     nextBlocks = [0,0,0]
  97.     nextBlocks[0] = getBlockType(pix[nextBlock1[0],nextBlock1[1]])
  98.     nextBlocks[1] = getBlockType(pix[nextBlock2[0],nextBlock2[1]])
  99.     nextBlocks[2] = getBlockType(pix[nextBlock3[0],nextBlock3[1]])
  100.     return(nextBlocks)
  101.  
  102. def getCurrnetBlock():
  103.     return(getBlockType(pix[currentBlock[0],currentBlock[1]]))
  104.  
  105. def getHoldBlock():
  106.     return(getBlockType(pix[holdBlock[0],holdBlock[1]]))
  107.  
  108. def autoCalibrate ():
  109.     topLeft  = pyautogui.locateOnScreen('hold.png')
  110.     bottomRight = pyautogui.locateOnScreen('next.png')
  111.  
  112.     x_pad = topLeft[0] - 62
  113.     y_pad = topLeft[1] - 47
  114.     x_width = bottomRight[0] + 226 #far right of image
  115.     y_length = bottomRight[1] + 550 #far bottom of image
  116.  
  117.     return (x_pad, y_pad, x_width, y_length)
  118.  
  119.  
  120. def startGame ():
  121.     x, y = pyautogui.locateCenterOnScreen('playButton.png')
  122.     pyautogui.click(x, y)  
  123.  
  124.  
  125. if __name__ == "__main__":
  126.  
  127.     sleep(5)
  128.     #im=ImageGrab.grab(bbox=(10,10,510,510)) # X1,Y1,X2,Y2
  129.  
  130.     x_pad, y_pad, x_width, y_length = autoCalibrate()
  131.     startGame()
  132.     sleep(2.36)
  133.  
  134.     #sleep(2)
  135.     im=ImageGrab.grab(bbox =(x_pad,y_pad,x_width,y_length))
  136.     #im.show()
  137.    
  138.     im.save("GAMESCREEN.png")
  139.     #im = Image.open("GAMESCREEN.png")
  140.     pix = im.load()
  141.  
  142.  
  143. #-------------------------------------
  144.     # test statement to get data.
  145.     print(getNextBlocks())
  146.     #print(getHoldBlock())
  147.     #print(getCurrnetBlock())
  148. #--------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment