Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
zesje
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Réouven ASSOULY
zesje
Commits
8fe5a902
Commit
8fe5a902
authored
7 years ago
by
Joseph Weston
Browse files
Options
Downloads
Patches
Plain Diff
add forgotten helper module
parent
0630d810
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
zesje/helpers/image_helper.py
+41
-0
41 additions, 0 deletions
zesje/helpers/image_helper.py
with
41 additions
and
0 deletions
zesje/helpers/image_helper.py
0 → 100644
+
41
−
0
View file @
8fe5a902
"""
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
]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment