Newer
Older
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 15 17:05:00 2020
@mattminder
"""
import os
import re
from skimage import io
import numpy as np
def load_image(path, ix=None):
"""Loads Image at specified path. Path can be to single image as supported
by skimage.io, or to folder containing images supported by skimage.io.
Ix specifies which image should be loaded, if None it loads all images."""
_, ext = os.path.splitext(path)
# Folder
if ext=='':
filelist = sorted(os.listdir(path))
filelist = [f for f in filelist if
re.search(r".png|.tif|.jpg|.bmp|.jpeg|.pbm|.pgm|.ppm|.pxm|.pnm|.jp2", f)]
filelist = [os.path.join(path, f) for f in filelist]
if len(filelist)==0:
raise ValueError('Folder does not contain images')
if ix is None:
ims = [io.imread(f) for f in filelist]
ims = np.array(ims)
return ims
else:
im = io.imread(filelist[ix])
return im
# File
else:
try:
im = io.imread(path)
except ValueError:
raise ValueError('Not an image file')
# Single image
if im.ndim == 2:
if ix is not None:
return im
else:
return im[None,:,:]
# Multistack image
if im.ndim == 3:
if ix is None:
return im
else:
return im[ix,:,:]