From d79fb355c92f240c7ad456199706cb76becea136 Mon Sep 17 00:00:00 2001
From: Bernardo TOD <bernardo.tod52@gmail.com>
Date: Thu, 23 May 2019 11:48:35 +0200
Subject: [PATCH] topng return width and height

---
 topng.py      |  6 ++++++
 totraindir.py | 27 +++++++++++++++++++++++----
 2 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/topng.py b/topng.py
index 9651446..c9b72d9 100644
--- a/topng.py
+++ b/topng.py
@@ -40,6 +40,9 @@ def getdir(filepath):
 	return '/'.join(filepath.split('/')[:-1]) + '/'
 
 def topng(inputfile, outfile=None, overwrite=True):
+	"""
+		return (64, 64) : the width and height
+	"""
 	try:
 		dicimg = pydicom.read_file(inputfile) # read dicom image
 	except pydicom.errors.InvalidDicomError as e:
@@ -48,6 +51,7 @@ def topng(inputfile, outfile=None, overwrite=True):
 	img = dicimg.pixel_array# get image array (12bits)
 
 	# test <<
+	# return img.shape # $$ COMMENT OR REMOVE THIS LINE 
 	print('img', img)
 	# print('img', type(img))
 	# print('min', img.min(), 'max', img.max())
@@ -82,6 +86,8 @@ def topng(inputfile, outfile=None, overwrite=True):
 
 	cv2.imwrite(savepath, img, [cv2.IMWRITE_PNG_COMPRESSION, 0]) # write png image
 
+	# test
+	return img.shape
 
 def topngs(inputdir, outdir):
 	"""
diff --git a/totraindir.py b/totraindir.py
index 4917b56..0fead80 100644
--- a/totraindir.py
+++ b/totraindir.py
@@ -22,6 +22,11 @@ def get(l, i, r):
 
 if __name__ == '__main__':
 	l = sorted(ls(GLOB_DIR))
+
+	# Initiliaze 
+	wmin = hmin = None # None is important here, confert the 'minimum' algo
+	wmax = hmax = w = h = 0
+
 	for cas in l[START:END]:# cas like 'Case0002'
 		caspath = join(GLOB_DIR, cas)
 
@@ -58,7 +63,6 @@ if __name__ == '__main__':
 				continue
 				pass
 
-
 			for i, dic in enumerate(l1):
 				# print('join', r, l2[i])
 				ref = join(r, l2[i]) # logically, should be the json ref of i dicom image
@@ -68,10 +72,25 @@ if __name__ == '__main__':
 					# print("infarctus:", infarctus) # Testing..
 					# topng(join(caspath, dic), '%/%-%' % (join(OUT_DIR, 'infarctus'), cas, dic)
 					# print(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'infarctus'), cas, dic)) # Testing..
-					topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'infarctus'), cas, dic))
+					w, h = topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'infarctus'), cas, dic))
 				else:
 					# print("no infarctus:", infarctus) # Testing..
 					# print(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'noinfarctus'), cas, dic)) # Testing..
-					topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'noinfarctus'), cas, dic))
+					w, h = topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'noinfarctus'), cas, dic))
+
+				# search maximums
+				if wmax < w: wmax = w 
+				if hmax < h: hmax = h
+
+				# search width minimum
+				if wmin is None: wmin = w
+				elif wmin > w: wmin = w
+				
+				# search height minimum
+				if hmin is None: hmin = h
+				elif hmin > h: hmin = h
+
+	print('min-width, max-width:', (wmin, wmax))
+	print('min-height, max-height:', (hmin, hmax))
 
-	print('Ended!')
\ No newline at end of file
+	print('Ended!')
-- 
2.39.5