image¶
Utilities for loading, resizing, and dewarping page images.
This module includes:
- A simple helper function (
imgsize) to format an image's width/height into a string. - A function (
get_page_dims) to optimize final page dimensions via the cubic model. - A class (
WarpedImage) that loads an image, resizes it, finds page boundaries, and threshold-remaps the final dewarped image to disk.
WarpedImage ¶
Handles loading, resizing, and thresholding a page image.
This class reads an image from disk, optionally downsamples it for display, detects page boundaries, and can threshold-remap the final dewarped image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
imgfile
|
|
Path to the image file to load. |
required |
config
|
|
A |
|
Source code in src/page_dewarp/image.py
resized
property
¶
Return a formatted string 'widthxheight' for the downsampled (small) image.
threshold ¶
Construct a dewarped, thresholded image using the RemappedImage class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page_dims
|
|
The final (height, width) dimensions for the page layout. |
required |
params
|
|
The optimization parameters (e.g. rotation, translation, cubic slopes). |
required |
Source code in src/page_dewarp/image.py
iteratively_assemble_spans ¶
Assemble spans from contours; fallback to line detection if too few are found.
First tries text contours to assemble spans. If fewer than three spans are found, attempts line detection (borders of a table box) rather than text detection, then re-assembles spans.
Source code in src/page_dewarp/image.py
attempt_reassemble_spans ¶
Attempt line-based re-assembly of spans, returning whichever set is larger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prev_spans
|
|
The spans identified by text contour detection. |
required |
Returns:
| Type | Description |
|---|---|
|
The new line-detected spans if larger in number; else the original spans. |
Source code in src/page_dewarp/image.py
resize_to_screen ¶
Downsample the loaded image to fit within SCREEN_MAX_W/H if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
copy
|
|
If True, returns a copy even if no resizing is needed. |
False
|
Returns:
| Type | Description |
|---|---|
|
A potentially resized NumPy array. |
|
If the image is already smaller than SCREEN_MAX_W/H, |
|
the same array (or its copy) is returned. |
Source code in src/page_dewarp/image.py
calculate_page_extents ¶
Create a mask for the page region, ignoring margins around the edges.
Source code in src/page_dewarp/image.py
contour_info ¶
Compute contour information for either text or line detection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
|
If True, identifies text contours; otherwise detects lines. |
True
|
Returns:
| Type | Description |
|---|---|
|
A list of contour objects (ContourInfo instances). |
Source code in src/page_dewarp/image.py
imgsize ¶
get_page_dims ¶
Optimize final page dimensions using a cubic polynomial model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corners
|
|
The four corner points of the page outline, in reduced coordinates. |
required |
rough_dims
|
|
An initial (height, width) estimate for page dimensions. |
required |
params
|
|
The optimization parameter vector, e.g. includes rotation/translation/cubic slopes. |
required |
Returns:
| Type | Description |
|---|---|
|
A 1D array of floats [height, width] representing the optimized page dimensions. |