Skip to content
Snippets Groups Projects
Commit 8fe5a902 authored by Joseph Weston's avatar Joseph Weston
Browse files

add forgotten helper module

parent 0630d810
No related branches found
No related tags found
No related merge requests found
"""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]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment