-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcheck_image_size.py
67 lines (60 loc) · 2.4 KB
/
check_image_size.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
# check_image_size: to see how to break large image data
#
#
#
# ----------------------------------------------------------------------------
# CDeep3M -- NCMIR/NBCR, UCSD -- Author: M Haberl -- Date: 02/2019
# -----------------------------------------------------------------------------
import os
import h5py
from PIL import Image
import numpy as np
import cv2
from read_files_in_folder import read_files_in_folder
Image.MAX_IMAGE_PIXELS = 10000000000000
def check_image_size(img_path):
print('Check image size of: ', img_path)
# check if a folder of png/tif files or a single stack to load
if os.path.isfile(img_path) == 1:
filename, file_extension = os.path.splitext(img_path)
if file_extension == '.h5':
print('Reading H5 image file')
data = h5py.File(img_path, 'r')
keys = list(data.keys())
imagesize = data[keys[0]].shape
return imagesize
elif file_extension.lower() in ['.tif', '.tiff']:
retflag, im = cv2.imreadmulti(img_path, flags=cv2.IMREAD_UNCHANGED)
if retflag is True:
imarray = np.array(im)
imagesize = im.shape
return (imagesize[1], imagesize[2], imagesize[0])
else:
raise Exception("Something went wrong while loading the multipage TIF file")
'''
elif file_extension == '.png':
im = Image.open(img_path)
imarray = np.array(im)
imagesize = imarray.shape
return imagesize
'''
elif os.path.isdir(img_path) == 1:
file_list = read_files_in_folder(img_path)[0]
png_list = [f for f in file_list if f.lower().endswith('.png')]
tif_list = [f for f in file_list if f.lower().endswith(('.tif', '.tiff'))]
if len(tif_list) + len(png_list) == 0:
print('No Tifs or PNGs found in the directory')
return
else:
# only read tif or pngs if ambiguous
if len(png_list) > len(tif_list):
file_list = png_list
else:
file_list = tif_list
filename = os.path.join(img_path, file_list[0])
print('Reading file: ', filename)
imarray = cv2.imread(filename, -1)
imagesize = (imarray.shape[0], imarray.shape[1], len(file_list))
return imagesize
else:
raise Exception('No images found')