From 3a13532849dd2e41595754af802692b027d5717a Mon Sep 17 00:00:00 2001 From: Denis Fedoseev Date: Mon, 2 Oct 2023 04:01:05 +0300 Subject: [PATCH] In search of the center --- process_image.py | 111 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 process_image.py diff --git a/process_image.py b/process_image.py new file mode 100644 index 0000000..f72585b --- /dev/null +++ b/process_image.py @@ -0,0 +1,111 @@ +import getopt +import sys +import os +from PIL import Image + +min_threshold = 200 +scan_grid_size = 1 + +options = "hi:" +long_options = ["help", "image="] + +argumentList = sys.argv[1:] +arguments, values = getopt.getopt(argumentList, options, long_options) + +image_name = None + +for currentArgument, currentValue in arguments: + + if currentArgument in ("-h", "--help"): + print("reader.py --image=image.jpg") + + sys.exit(0) + + elif currentArgument in ("-i", "--image"): + image_name = currentValue + +print('image name: ' + image_name) + +f, e = os.path.splitext(image_name) + + +def convert_coordinates(coords, grid_size): + grid_x_center = round(grid_size[0] / 2) + grid_y_center = round(grid_size[1] / 2) + + new_circle_x = coords[0] - grid_x_center + new_circle_y = coords[1] - grid_y_center + + return new_circle_x, new_circle_y + + +def find_circle(image): + px = image.load() + im_size = image.size + print("size: ", im_size) + + pixel_coords = { + 'max_top': {}, + 'max_bottom': {}, + 'cur_line_max_bottom': {}, + 'prev_line_max_bottom': {} + } + + is_prev_line_null = 1 + + for y in range(0, im_size[1], scan_grid_size): + # print("{:3}: ".format(y), end="") + is_current_line_not_null = 0 + + for x in range(0, im_size[0], scan_grid_size): + px_colors = px[x, y] + intens = 0 if px_colors[1] <= min_threshold else px_colors[1] + + is_current_line_not_null = is_current_line_not_null + intens + + if is_prev_line_null: + if intens > 0: + top_max_intense = 0 if not pixel_coords["max_top"].get("intense") else pixel_coords["max_top"].get( + "intense") + if intens > top_max_intense: + pixel_coords["max_top"] = {"intense": intens, "x": x, "y": y} + + max_bottom_intense = 0 if not pixel_coords["cur_line_max_bottom"].get("intense") else pixel_coords[ + "cur_line_max_bottom"].get("intense") + + if intens > 0 and intens > max_bottom_intense: + pixel_coords["cur_line_max_bottom"] = {"intense": intens, "x": x, "y": y} + + # print('{:4}'.format(intens), end="") + + if is_current_line_not_null > 0: + is_prev_line_null = 0 + pixel_coords["prev_line_max_bottom"] = pixel_coords["cur_line_max_bottom"] + pixel_coords["cur_line_max_bottom"] = {} + else: + if not pixel_coords["max_bottom"]: + pixel_coords["max_bottom"] = pixel_coords["prev_line_max_bottom"] + + # print("") + + print("") + + print("coords: ", pixel_coords) + x_center = round((pixel_coords["max_bottom"]["x"] + pixel_coords["max_top"]["x"]) / 2) + y_center = round((pixel_coords["max_bottom"]["y"] + pixel_coords["max_top"]["y"]) / 2) + + print("center x: {:3} y: {:3}".format(x_center, y_center)) + + coords = convert_coordinates((x_center, y_center), im_size) + return coords + + +try: + with Image.open(image_name) as img: + + circle_on_image_coords = find_circle(img) + + print("center coords: ", circle_on_image_coords) + +except OSError: + print("cannot open", image_name)