diff --git a/NCISlideUtil.py b/NCISlideUtil.py index 07c325e..b33e17a 100644 --- a/NCISlideUtil.py +++ b/NCISlideUtil.py @@ -9,6 +9,7 @@ from dev_utils import file_md5 from dev_utils import postslide from dev_utils import post_url +from make_thumbs import make_thumbnails # GLOBALS (for now) # config = {'thumbnail_size': 100, 'thread_limit': 20} @@ -174,3 +175,4 @@ def addSpecialty(data): # run process on each image res = ThreadPool(thread_limit).imap_unordered(process, manifest) print([r for r in res]) + make_thumbnails() diff --git a/make_thumbs.py b/make_thumbs.py index 22523cc..0d1bf8a 100644 --- a/make_thumbs.py +++ b/make_thumbs.py @@ -1,17 +1,24 @@ import requests import openslide import pycurl -from multiprocessing.pool import ThreadPool +import os SLIDE_LIST_URL = "http://ca-back:4010/data/Slide/find" IIP_BASE = "http://ca-back:4010/img/IIP/raw/?FIF=" UPDATE_URL = "http://ca-back:4010/data/Slide/update" # TODO -- token input? IM_SIZE = 256 -THREADS = 5 -REGNERATE = False +REGNERATE = True SAVE_DIR = "/images/thumbnails/" +# make this SAVE_DIR if it does not exist +try: + os.mkdir(SAVE_DIR) + print("created thumbnail SAVE_DIR at", SAVE_DIR) +except FileExistsError: + pass + + def setThumb(id, val): requests.post(UPDATE_URL + "?_id=" + id, json={'thumbnail': val}) @@ -26,10 +33,11 @@ def process(record): # skip ones which already have a thumbnail, unless otherwise specified if REGNERATE or not record.get("thumbnail", False): try: - slide = openslide.OpenSlide(file) - gen_thumbnail(name, slide, IM_SIZE, imgtype="png") - setThumb(record['_id']["$oid"], name+".png") - return "" + with openslide.OpenSlide(file) as slide: + gen_thumbnail(name, slide, IM_SIZE, imgtype="png") + setThumb(record['_id']["$oid"], name+".png") + # return empty to denote no issue. + return "" except BaseException as e: try: url = IIP_BASE + file + "&WID=200&CVT=png" @@ -42,9 +50,11 @@ def process(record): except BaseException as y: return [name, y] -# do it -manifest = requests.get(SLIDE_LIST_URL).json() -print(manifest[0]) +def make_thumbnails(): + manifest = requests.get(SLIDE_LIST_URL).json() + print(manifest[0]) + res = [process(x) for x in manifest] + print([x for x in filter(None,[r for r in res])]) -res = ThreadPool(THREADS).imap_unordered(process, manifest) -print([x for x in filter(None,[r for r in res])]) +if __name__ == "__main__": + make_thumbnails()