-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.py
74 lines (46 loc) · 1.71 KB
/
extract.py
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
import numpy as np
import pandas as pd
from PIL import Image
Image.MAX_IMAGE_PIXELS = None
try:
from osgeo import gdal
def get_cores(path, results):
from osgeo import gdal
slide = gdal.Open(path)
b1 = slide.GetRasterBand(1)
b2 = slide.GetRasterBand(2)
b3 = slide.GetRasterBand(3)
for name, c, r, x, y, w, h, QC_pass in results.itertuples(name=None):
core = np.zeros((h, w, 3), dtype='uint8')
core[..., 0] = b1.ReadAsArray(x, y, w, h)
core[..., 1] = b2.ReadAsArray(x, y, w, h)
core[..., 2] = b3.ReadAsArray(x, y, w, h)
yield c, r, Image.fromarray(core)
except:
def get_cores(path, results):
img = Image.open(path).convert('RGB')
img = np.asarray(img)
for name, c, r, x, y, w, h, QC_pass in results.itertuples(name=None):
yield c, r, Image.fromarray(img[y:y+h, x:x+w])
if __name__ == '__main__':
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
'path',
type=str,
default='/path_to_TMA_img/',
help='TMA image path.'
)
parser.add_argument(
'-q', '--quality',
type=int,
default=70,
help='quality of JPEG images to be saved.'
)
args, _ = parser.parse_known_args()
results = pd.read_csv(os.path.splitext(args.path)[0] + '.csv', index_col='name')
output_dir = os.path.splitext(args.path)[0]
os.makedirs(output_dir, exist_ok=True)
for c, r, img in get_cores(args.path, results):
img.save(os.path.join(output_dir, f'{c}_{r}.jpg'), quality=args.quality)