Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. def FindColorIn(r,g,b, xmin, xmax, ymin, ymax):
  2. image = ImageGrab.grab()
  3. for x in range(xmin, xmax):
  4. for y in range(ymin,ymax):
  5. px = image.getpixel((x, y))
  6. if px[0] == r and px[1] == g and px[2] == b:
  7. return x, y
  8.  
  9. def FindColor(r,g,b):
  10. image = ImageGrab.grab()
  11. size = image.size
  12. pos = FindColorIn(r,g,b, 1, size[0], 1, size[1])
  13. return pos
  14.  
  15. if ((X-X1)^2 + (Y-Y1)^2 + (Z-Z1)^2) <= (Tol^2) then
  16. ...
  17.  
  18. typedef struct {
  19. unsigned char r, g, b;
  20. } RGB;
  21.  
  22. double ColourDistance(RGB e1, RGB e2)
  23. {
  24. long rmean = ( (long)e1.r + (long)e2.r ) / 2;
  25. long r = (long)e1.r - (long)e2.r;
  26. long g = (long)e1.g - (long)e2.g;
  27. long b = (long)e1.b - (long)e2.b;
  28. return sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8));
  29. }
  30.  
  31. def ColorDistance(rgb1,rgb2):
  32. '''d = {} distance between two colors(3)'''
  33. rm = 0.5*(rgb1[0]+rgb2[0])
  34. d = sum((2+rm,4,3-rm)*(rgb1-rgb2)**2)**0.5
  35. return d
  36.  
  37. >>> import numpy
  38. >>> rgb1 = numpy.array([1,1,0])
  39. >>> rgb2 = numpy.array([0,0,0])
  40. >>> ColorDistance(rgb1,rgb2)
  41. 2.5495097567963922
  42.  
  43. if abs(px[0]- r) <= rtol and
  44. abs(px[1]- g) <= gtol and
  45. abs(px[2]- b) <= btol:
  46. return x, y
  47.  
  48. if px[0] == r and px[1] == g and px[2] == b:
  49.  
  50. if max(map(lambda a,b: abs(a-b), px, (r,g,b))) < tolerance:
  51.  
  52. def eq_with_tolerance(a, b, t):
  53. return a-t <= b <= a+t
  54.  
  55. def FindColorIn(r,g,b, xmin, xmax, ymin, ymax, tolerance=0):
  56. image = ImageGrab.grab()
  57. for x in range(xmin, xmax):
  58. for y in range(ymin,ymax):
  59. px = image.getpixel((x, y))
  60. if eq_with_tolerance(r, px[0], tolerance) and eq_with_tolerance(g, px[1], tolerance) and eq_with_tolerance(b, px[2], tolerance):
  61. return x, y
  62.  
  63. def pixelMatchesColor(x, y, expectedRGBColor, tolerance=0):
  64. r, g, b = screenshot().getpixel((x, y))
  65. exR, exG, exB = expectedRGBColor
  66.  
  67. return (abs(r - exR) <= tolerance) and (abs(g - exG) <= tolerance) and (abs(b - exB) <= tolerance)
  68.  
  69. def color_distance(rgb1, rgb2):
  70. rm = 0.5 * (rgb1[0] + rgb2[0])
  71. rd = ((2 + rm) * (rgb1[0] - rgb2[0])) ** 2
  72. gd = (4 * (rgb1[1] - rgb2[1])) ** 2
  73. bd = ((3 - rm) * (rgb1[2] - rgb2[2])) ** 2
  74. return (rd + gd + bd) ** 0.5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement