Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from PIL import Image, ImageOps
- def detect(images):
- test_image = Image.open(images)
- test_image = ImageOps.grayscale(test_image) # Converting to grayscale
- test_image = test_image.resize((500, 500))
- test_image = np.array(test_image)
- # To find those pixels which are different from black
- pts = np.argwhere(test_image > 0)
- ymin, xmin = pts.min(axis=0)
- ymax, xmax = pts.max(axis=0)
- # If the distance between the extreme points in X Direction is more than that in Y direction, then the arrow lies
- # in the horizontal direction, otherwise in the vertical direction
- X_dist = xmax - xmin
- Y_dist = ymax - ymin
- if X_dist > Y_dist:
- count1 = np.count_nonzero(np.where(pts == xmax))
- count2 = np.count_nonzero(np.where(pts == xmin))
- # If on the X axis there are greater number of points which are farther from the left edge of the window then
- # it means that the arrow is pointing left and vice versa
- if count1 > count2:
- return "LEFT"
- else:
- return "RIGHT"
- else:
- count1 = np.count_nonzero(np.where(pts == ymax))
- count2 = np.count_nonzero(np.where(pts == ymin))
- if count1 > count2:
- return "UP"
- else:
- return "DOWN"
- if __name__ == '__main__':
- print("Enter the image file: ")
- images = input().split(" ")
- if len(images) == 1:
- ans = detect(images[0])
- print(ans)
- else:
- for i in images:
- with open("output.txt", 'a') as f:
- ans = detect(i)
- f.write(f"{ans}\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement