From 8fe5a902699ee665f6d371efde02ae491f730dc0 Mon Sep 17 00:00:00 2001 From: Joseph Weston <joseph@weston.cloud> Date: Fri, 9 Mar 2018 14:24:12 +0100 Subject: [PATCH] add forgotten helper module --- zesje/helpers/image_helper.py | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 zesje/helpers/image_helper.py diff --git a/zesje/helpers/image_helper.py b/zesje/helpers/image_helper.py new file mode 100644 index 000000000..3475b910e --- /dev/null +++ b/zesje/helpers/image_helper.py @@ -0,0 +1,41 @@ +"""Utilities for dealing with images""" + +import numpy as np +import cv2 + +def get_widget_image(image_path, widget): + box = (widget.top, widget.bottom, widget.left, widget.right) + raw_image = get_box(cv2.imread(image_path), box, padding=0.3) + return cv2.imencode(".jpg", raw_image)[1].tostring() + + +def guess_dpi(image_array): + h, *_ = image_array.shape + resolutions = np.array([1200, 600, 300, 200, 150, 120, 100, 75, 60, 50, 40]) + return resolutions[np.argmin(abs(resolutions - 25.4 * h / 297))] + + +def get_box(image_array, box, padding): + """Extract a subblock from an array corresponding to a scanned A4 page. + + Parameters: + ----------- + image_array : 2D or 3D array + The image source. + box : 4 floats (top, bottom, left, right) + Coordinates of the bounding box in inches. By due to differing + traditions, box coordinates are counted from the bottom left of the + image, while image array coordinates are from the top left. + padding : float + Padding around box borders in inches. + """ + h, w, *_ = image_array.shape + dpi = guess_dpi(image_array) + box = np.array(box) + box += (padding, -padding, -padding, padding) + box = (np.array(box) * dpi).astype(int) + # Here we are not returning the lowest pixel of the image because otherwise + # the numpy slicing is not correct. + top, bottom = min(h, box[0]), max(1, box[1]) + left, right = max(0, box[2]), min(w, box[3]) + return image_array[-top:-bottom, left:right] -- GitLab