From 7f1bf9341f00334af0810b9097f4aef48c51c6d6 Mon Sep 17 00:00:00 2001 From: mattminder <myfiles@Mattus-MacBook-Pro.local> Date: Sat, 16 May 2020 13:47:16 +0200 Subject: [PATCH] image loader --- disk/image_loader.py | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 disk/image_loader.py diff --git a/disk/image_loader.py b/disk/image_loader.py new file mode 100644 index 0000000..1f96666 --- /dev/null +++ b/disk/image_loader.py @@ -0,0 +1,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,:,:] + -- GitLab