This repository was archived by the owner on May 14, 2025. It is now read-only.
forked from marta-seq/PENGUIN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageParser.py
More file actions
163 lines (134 loc) · 4.67 KB
/
ImageParser.py
File metadata and controls
163 lines (134 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from apeer_ometiff_library import io
# import tensorflow as tf
import numpy as np
from src.file_specs import FileSpecifics
from tifffile import tifffile
# class ImageParser():
# def parse_image(img_path: str) -> dict:
def parse_image(img_path: str) -> np.array:
"""
Load an image from the specified path, process it, and return it as a NumPy array.
The function reads a TIFF image, squeezes the array to remove single-dimensional entries,
and moves the axis to ensure the array has dimensions (X, Y, channels).
Parameters
----------
img_path : str
Path to the image file (not the mask).
Returns
-------
np.ndarray or None
A NumPy array representing the image with shape (X, Y, channels).
Returns None if there is an error reading the image.
Notes
-----
This function is designed to handle OME-TIFF images specifically. If the image cannot be read,
the function will print an error message and return None.
Examples
--------
>>> img_array = parse_image('path/to/image.ome.tiff')
>>> if img_array is not None:
>>> print("Image shape:", img_array.shape)
"""
try:
(img_apeer, omexml) = io.read_ometiff(img_path)
img = np.squeeze(img_apeer)
img = np.moveaxis(img, 0, -1)
# return {'image': img, 'img_meta': omexml, 'filename': img_path}
return img
except Exception as e:
print('did not read image', img_path)
print(e)
return None
def parse_image_pages(img_path: str) -> np.array:
"""
Load a TIFF file with multiple pages and return it as a NumPy array.
This function reads a multi-page TIFF file, extracts each page as an image,
and combines them into a single NumPy array with dimensions (X, Y, channels).
Parameters
----------
img_path : str
Path to the TIFF image file.
Returns
-------
np.ndarray
A NumPy array representing the image with shape (X, Y, channels).
Examples
--------
>>> img_array = parse_image_pages('path/to/image.tiff')
>>> print("Image shape:", img_array.shape)
"""
im = []
with tifffile.TiffFile(img_path) as tif:
for page in tif.pages:
image = page.asarray()
im.append(image)
im = np.array(im)
img = np.moveaxis(im, 0, -1)
return img
def parse_image_pages_namesCH(img_path):
"""
Load a TIFF file with multiple pages and retrieve the names of the pages.
This function reads a multi-page TIFF file, extracts each page as an image,
and retrieves the names of the channels from the page tags. The images are
combined into a single NumPy array with dimensions (X, Y, channels).
Parameters
----------
img_path : str
Path to the TIFF image file.
Returns
-------
tuple[np.ndarray, list[str]]
A tuple containing:
- A NumPy array representing the image with shape (X, Y, channels).
- A list of strings representing the names of the channels.
Notes
-----
The function looks for 'PageName' or 'ImageDescription' tags in each page
to determine the channel names. If neither tag is found, the channel name
is not added to the list.
Examples
--------
>>> img_array, channel_names = parse_image_pages_namesCH('path/to/image.tiff')
>>> if img_array is not None:
>>> print("Image shape:", img_array.shape)
>>> print("Channel names:", channel_names)
"""
im = []
ch_names = []
with tifffile.TiffFile(img_path) as tif:
for page in tif.pages:
image = page.asarray()
im.append(image)
if 'PageName' in page.tags:
ch_names.append(page.tags['PageName'].value)
elif 'ImageDescription' in page.tags:
ch_names.append(page.tags['ImageDescription'].value)
else:
pass
im = np.array(im)
img = np.moveaxis(im, 0, -1)
return img, ch_names
def parse_image_with_meta(img_path: str) -> dict:
"""
Load an image and its annotation and returns
a dictionary with image and metadata
Parameters
----------
CHANNELS
img_path : str
Image location.
Returns
-------
dict
Dictionary mapping an image and its annotation.
"""
try:
(img_apeer, omexml) = io.read_ometiff(img_path)
img = np.squeeze(img_apeer)
img = np.moveaxis(img, 0, -1)
# return {'image': img, 'img_meta': omexml, 'filename': img_path}
return img
except Exception as e:
print('did not read image', img_path)
print(e)
return {'img_meta': omexml, 'filename': img_path, 'shape_img': img.shape, 'img':img}