Advertisement
KunalA18

Om-task2

Jul 9th, 2022
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import numpy as np
  2. from PIL import Image, ImageOps
  3.  
  4.  
  5. def detect(images):
  6. test_image = Image.open(images)
  7. test_image = ImageOps.grayscale(test_image) # Converting to grayscale
  8. test_image = test_image.resize((500, 500))
  9. test_image = np.array(test_image)
  10.  
  11. # To find those pixels which are different from black
  12. pts = np.argwhere(test_image > 0)
  13. ymin, xmin = pts.min(axis=0)
  14. ymax, xmax = pts.max(axis=0)
  15.  
  16. # If the distance between the extreme points in X Direction is more than that in Y direction, then the arrow lies
  17. # in the horizontal direction, otherwise in the vertical direction
  18. X_dist = xmax - xmin
  19. Y_dist = ymax - ymin
  20. if X_dist > Y_dist:
  21. count1 = np.count_nonzero(np.where(pts == xmax))
  22. count2 = np.count_nonzero(np.where(pts == xmin))
  23. # If on the X axis there are greater number of points which are farther from the left edge of the window then
  24. # it means that the arrow is pointing left and vice versa
  25. if count1 > count2:
  26. return "LEFT"
  27. else:
  28. return "RIGHT"
  29. else:
  30. count1 = np.count_nonzero(np.where(pts == ymax))
  31. count2 = np.count_nonzero(np.where(pts == ymin))
  32. if count1 > count2:
  33. return "UP"
  34. else:
  35. return "DOWN"
  36.  
  37.  
  38. if __name__ == '__main__':
  39. print("Enter the image file: ")
  40. images = input().split(" ")
  41. if len(images) == 1:
  42. ans = detect(images[0])
  43. print(ans)
  44. else:
  45. for i in images:
  46. with open("output.txt", 'a') as f:
  47. ans = detect(i)
  48. f.write(f"{ans}\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement