Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
python3-imagej-tiff
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Elphel
python3-imagej-tiff
Commits
8b48ba4a
Commit
8b48ba4a
authored
Jul 02, 2018
by
Oleg Dzhimiev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tested only for (t,z,h,w,c) shapes
parent
0a742391
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
19 deletions
+69
-19
imagej_tiffwriter.py
imagej_tiffwriter.py
+69
-19
No files found.
imagej_tiffwriter.py
View file @
8b48ba4a
...
@@ -24,19 +24,52 @@ __copyright__ = "Copyright 2018, Elphel, Inc."
...
@@ -24,19 +24,52 @@ __copyright__ = "Copyright 2018, Elphel, Inc."
__license__
=
"GPL-3.0+"
__license__
=
"GPL-3.0+"
__email__
=
"oleg@elphel.com"
__email__
=
"oleg@elphel.com"
'''
Usage example:
import imagej_tiffwriter
import numpy as np
# have a few images in the form of numpy arrays
# make sure to stack them as:
# - (t,z,h,w,c)
# - (z,h,w,c)
# - (h,w,c)
# - (h,w)
imagej_tiffwriter.save(path,images)
'''
from
PIL
import
Image
,
TiffImagePlugin
from
PIL
import
Image
,
TiffImagePlugin
import
numpy
as
np
import
numpy
as
np
import
math
import
math
# DO NOT USE?
# thing is the old ImageJs <1.52d poorly handle tags directories or something like that
def
__get_IJ_IFD
(
t
,
z
,
c
):
def
__get_IJ_IFD
(
t
,
z
,
c
):
ifd
=
TiffImagePlugin
.
ImageFileDirectory_v2
()
ifd
=
TiffImagePlugin
.
ImageFileDirectory_v2
()
ijheader
=
[
'ImageJ='
,
'hyperstack=true'
,
'images='
+
str
(
t
*
z
*
c
),
'channels='
+
str
(
c
),
'slices='
+
str
(
z
),
'frames='
+
str
(
t
),
'loop=false'
]
ifd
[
270
]
=
(
"
\n
"
.
join
(
ijheader
)
+
"
\n
"
)
#is_hyperstack = 'true' if len(shape)>1 else 'false'
#is_hyperstack = 'true' if len(shape)>1 else 'false'
#if (len(shape)>0):
#if (len(shape)>0):
return
ifd
return
ifd
def
save
(
path
,
images
,
force_stack
=
False
,
force_hyperstack
=
False
):
#def save(path,images,force_stack=False,force_hyperstack=False):
def
save
(
path
,
images
):
# Got images, analyze shape:
# Got images, analyze shape:
# - possible formats (c == depth):
# - possible formats (c == depth):
...
@@ -45,35 +78,52 @@ def save(path,images,force_stack=False,force_hyperstack=False):
...
@@ -45,35 +78,52 @@ def save(path,images,force_stack=False,force_hyperstack=False):
# -- (h,w,c)
# -- (h,w,c)
# -- (h,w)
# -- (h,w)
# save single layer image
# 0 or 1 images.shapes are not handled
#
# save single channel image in the form of numpy array
# -
if
len
(
images
.
shape
)
==
2
:
if
len
(
images
.
shape
)
==
2
:
# (h,w) -> (h,w,c=1)
# (h,w) -> (h,w,c=1)
images
=
images
[:,:,
np
.
newaxis
]
image
=
Image
.
fromarray
(
images
)
image
.
save
(
path
)
elif
len
(
images
.
shape
)
>
2
:
elif
len
(
images
.
shape
)
>
2
:
# do nothing
pass
else
:
# (w,) -> (h=1,w,c=1)
images
=
images
[
np
.
newaxis
,:,
np
.
newaxis
]
#imlist = np.ravel(Image.fromarray(images))
h
,
w
,
c
=
images
.
shape
[
-
3
:]
t
,
z
,
h
,
w
,
c
=
images
.
shape
c_axis
=
len
(
images
.
shape
)
-
1
if
len
(
images
.
shape
)
==
3
:
channels
=
np
.
squeeze
(
np
.
split
(
images
,
c
,
axis
=
c_axis
))
images
=
np
.
reshape
(
images
,(
1
,
h
,
w
,
c
))
z
=
images
.
shape
[
-
4
]
if
len
(
images
.
shape
)
==
4
:
images
=
np
.
reshape
(
images
,(
1
,
z
,
h
,
w
,
c
))
split_channels
=
np
.
concatenate
(
channels
,
axis
=-
3
)
t
=
images
.
shape
[
-
5
]
images_flat
=
np
.
reshape
(
split_channels
,(
-
1
,
h
,
w
))
imlist
=
[]
c_axis
=
len
(
images
.
shape
)
-
1
for
i
in
range
(
images_flat
.
shape
[
0
]):
imlist
.
append
(
Image
.
fromarray
(
images_flat
[
i
]))
imlist
[
0
]
.
save
(
path
,
save_all
=
True
,
append_images
=
imlist
[
1
:],
tiffinfo
=
__get_IJ_IFD
(
t
,
z
,
c
))
if
c
>
1
:
channels
=
np
.
squeeze
(
np
.
split
(
images
,
c
,
axis
=
c_axis
))
else
:
channels
=
np
.
squeeze
(
images
,
axis
=
c_axis
)
split_channels
=
np
.
concatenate
(
channels
,
axis
=-
3
)
images_flat
=
np
.
reshape
(
split_channels
,(
-
1
,
h
,
w
))
imlist
=
[]
for
i
in
range
(
images_flat
.
shape
[
0
]):
imlist
.
append
(
Image
.
fromarray
(
images_flat
[
i
]))
imlist
[
0
]
.
save
(
path
,
save_all
=
True
,
append_images
=
imlist
[
1
:])
# thing is the old ImageJs <1.52d poorly handle tags directories or something like that
#imlist[0].save(path,save_all=True,append_images=imlist[1:],tiffinfo=__get_IJ_IFD(t,z,c))
# Testing
# Testing
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
def
hamming_window
(
x
,
N
):
def
hamming_window
(
x
,
N
):
y
=
0.54
-
0.46
*
math
.
cos
(
2
*
math
.
pi
*
x
/
(
N
-
1
))
y
=
0.54
-
0.46
*
math
.
cos
(
2
*
math
.
pi
*
x
/
(
N
-
1
))
return
y
return
y
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment