Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
dumux
Manage
Activity
Members
Labels
Plan
Issues
77
Issue boards
Milestones
Wiki
Code
Merge requests
77
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
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
dumux-repositories
dumux
Commits
67cbdc07
Commit
67cbdc07
authored
5 months ago
by
Mathis Kelm
Browse files
Options
Downloads
Patches
Plain Diff
[PNM][util] Add script to read image stack into numpy array
parent
2a204815
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dumux/porenetwork/util/image_stack_to_npy.py
+74
-0
74 additions, 0 deletions
dumux/porenetwork/util/image_stack_to_npy.py
with
74 additions
and
0 deletions
dumux/porenetwork/util/image_stack_to_npy.py
0 → 100755
+
74
−
0
View file @
67cbdc07
#!/usr/bin/env python3
# SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
# SPDX-License-Identifier: GPL-3.0-or-later
"""
Reads a stack of binarized images representing slices of a 3D domain and stores the data
in a binary 3D numpy array.
"""
from
PIL
import
Image
import
numpy
as
np
import
os.path
import
sys
import
argparse
def
read_images
(
filenames
,
invert
=
False
):
arr3d
=
[]
for
filename
in
filenames
:
if
not
os
.
path
.
isfile
(
filename
):
print
(
f
"
\r
image
{
filename
}
not found
"
)
continue
message
=
f
"
reading image
{
filename
}
"
print
(
f
"
\r
{
message
}
"
,
end
=
""
)
im
=
Image
.
open
(
filename
)
a
=
np
.
asarray
(
im
)
im
.
close
()
arr3d
.
append
(
a
)
print
(
f
"
\r
{
'
'
*
len
(
message
)
}
"
,
end
=
""
)
arr3d
=
np
.
asarray
(
arr3d
)
/
255
if
invert
:
arr3d
=
1
-
arr3d
print
(
f
"
\r
Read
{
arr3d
.
shape
[
0
]
}
/
{
len
(
filenames
)
}
images.
"
)
return
arr3d
def
read_image_stack
(
format_string
,
n_start
,
n_end
,
invert
=
False
):
filenames
=
[
format_string
.
format
(
i
)
for
i
in
range
(
n_start
,
n_end
)]
return
read_images
(
filenames
,
invert
)
def
main
():
parser
=
argparse
.
ArgumentParser
(
description
=
"
Process a stack of images into a binary numpy array
"
)
parser
.
add_argument
(
"
outfile
"
,
help
=
"
filename of output file
"
)
parser
.
add_argument
(
"
filenames
"
,
metavar
=
"
files
"
,
nargs
=
"
*
"
,
help
=
"
filenames of images to include
"
)
parser
.
add_argument
(
"
--format
"
,
dest
=
"
format_string
"
,
nargs
=
"
?
"
,
help
=
"
format string with one unnamed field for slice index
"
,
)
parser
.
add_argument
(
"
--from
"
,
dest
=
"
start
"
,
type
=
int
,
nargs
=
"
?
"
,
default
=
0
,
help
=
"
starting index
"
)
parser
.
add_argument
(
"
--to
"
,
dest
=
"
end
"
,
type
=
int
,
nargs
=
"
?
"
,
default
=
0
,
help
=
"
ending index (exclusive)
"
)
parser
.
add_argument
(
"
--invert
"
,
dest
=
"
invert
"
,
action
=
"
store_true
"
,
default
=
False
,
help
=
"
invert pore and solid
"
)
args
=
parser
.
parse_args
()
filenames
=
args
.
filenames
if
args
.
format_string
:
filenames
+=
[
args
.
format_string
.
format
(
i
)
for
i
in
range
(
args
.
start
,
args
.
end
)]
array3d
=
read_images
(
filenames
,
args
.
invert
)
np
.
save
(
args
.
outfile
,
array3d
)
if
__name__
==
"
__main__
"
:
main
()
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