From 7fc8782c563394667ee85cc5d781addbcb662b14 Mon Sep 17 00:00:00 2001 From: yuxin wu Date: Fri, 28 Apr 2023 10:38:32 +0800 Subject: [PATCH 1/7] add script: convert jpg to tiff with meta data --- preprocessing/cvt_jpg2tiff.py | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 preprocessing/cvt_jpg2tiff.py diff --git a/preprocessing/cvt_jpg2tiff.py b/preprocessing/cvt_jpg2tiff.py new file mode 100644 index 0000000..5e724ea --- /dev/null +++ b/preprocessing/cvt_jpg2tiff.py @@ -0,0 +1,42 @@ +""" +1. install vips command line tool (ubuntu 18.04): + sudo apt update + sudo apt install libvips + sudo apt install libvips-tools + +2. install tiff-tools (ubuntu 18.04): + sudo apt update + sudo apt install libtiff-tools +""" +import os +import glob +import sys + +def cvt_jpg2tiff(src_dir, dst_dir): + if not os.path.exists(dst_dir): + os.makedirs(dst_dir) + + src_files = glob.glob(os.path.join(src_dir, '*.jpg')) + for src_file in src_files: + dst_file = os.path.join(dst_dir, os.path.basename(src_file).replace('.jpg', '.tiff')) + + # convert jpg to tiff + cmd = 'vips im_vips2tiff' + src_file + ' ' + dst_file + ':none,tile:256x256,pyramid' + print(cmd) + os.system(cmd) + + # add metadata to tiff + cmd = 'tifftools set -y -s ImageDescription "Iscan Coreo |AppMag = 20" ' + dst_file + print(cmd) + os.system(cmd) + print('Done.') + +if __name__ == '__main__': + args = sys.argv + if len(args) != 3: + print('Usage: python cvt_jpg2tiff.py src_dir dst_dir') + sys.exit(0) + src_dir = args[1] + dst_dir = args[2] + cvt_jpg2tiff(src_dir, dst_dir) + \ No newline at end of file From 6e380c71d68c572e7e6ef5100e032e3f58f0fd0e Mon Sep 17 00:00:00 2001 From: yuxin wu Date: Fri, 28 Apr 2023 11:01:31 +0800 Subject: [PATCH 2/7] update --- preprocessing/cvt_jpg2tiff.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/preprocessing/cvt_jpg2tiff.py b/preprocessing/cvt_jpg2tiff.py index 5e724ea..2d707a0 100644 --- a/preprocessing/cvt_jpg2tiff.py +++ b/preprocessing/cvt_jpg2tiff.py @@ -5,8 +5,7 @@ sudo apt install libvips-tools 2. install tiff-tools (ubuntu 18.04): - sudo apt update - sudo apt install libtiff-tools + pip install tifftools """ import os import glob @@ -21,7 +20,7 @@ def cvt_jpg2tiff(src_dir, dst_dir): dst_file = os.path.join(dst_dir, os.path.basename(src_file).replace('.jpg', '.tiff')) # convert jpg to tiff - cmd = 'vips im_vips2tiff' + src_file + ' ' + dst_file + ':none,tile:256x256,pyramid' + cmd = 'vips im_vips2tiff ' + src_file + ' ' + dst_file + ':none,tile:256x256,pyramid' print(cmd) os.system(cmd) From 58ed2620a93a408fe3f1f333e541a90859ee62f3 Mon Sep 17 00:00:00 2001 From: yuxin wu Date: Fri, 28 Apr 2023 11:09:00 +0800 Subject: [PATCH 3/7] update cmd from vips to convert for size limit --- preprocessing/cvt_jpg2tiff.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/preprocessing/cvt_jpg2tiff.py b/preprocessing/cvt_jpg2tiff.py index 2d707a0..a2e25e2 100644 --- a/preprocessing/cvt_jpg2tiff.py +++ b/preprocessing/cvt_jpg2tiff.py @@ -1,8 +1,6 @@ """ -1. install vips command line tool (ubuntu 18.04): - sudo apt update - sudo apt install libvips - sudo apt install libvips-tools +1. install convert: + https://github.com/ImageMagick/ImageMagick 2. install tiff-tools (ubuntu 18.04): pip install tifftools @@ -20,7 +18,8 @@ def cvt_jpg2tiff(src_dir, dst_dir): dst_file = os.path.join(dst_dir, os.path.basename(src_file).replace('.jpg', '.tiff')) # convert jpg to tiff - cmd = 'vips im_vips2tiff ' + src_file + ' ' + dst_file + ':none,tile:256x256,pyramid' + + cmd = f'convert {src_file} -define tiff64:tile-geometry=256x256 -compress none "ptif:{dst_file}"' print(cmd) os.system(cmd) From 2b7ed4a29ce09ce579c5a53a2d591f371bcabae5 Mon Sep 17 00:00:00 2001 From: yuxin wu Date: Fri, 28 Apr 2023 11:26:10 +0800 Subject: [PATCH 4/7] update comments --- preprocessing/cvt_jpg2tiff.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/preprocessing/cvt_jpg2tiff.py b/preprocessing/cvt_jpg2tiff.py index a2e25e2..91a0f5b 100644 --- a/preprocessing/cvt_jpg2tiff.py +++ b/preprocessing/cvt_jpg2tiff.py @@ -4,6 +4,10 @@ 2. install tiff-tools (ubuntu 18.04): pip install tifftools + +If you have size limit error, try to increase the limit in: /etc/ImageMagick-6/policy.xml +check: https://github.com/ImageMagick/ImageMagick/issues/396#issuecomment-319569255 + """ import os import glob From 4a5efc1df2891415c70e3a25c3edc6536ec5d86e Mon Sep 17 00:00:00 2001 From: yuxin wu Date: Fri, 28 Apr 2023 14:12:31 +0800 Subject: [PATCH 5/7] use pyvips instead --- preprocessing/cvt_jpg2tiff.py | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/preprocessing/cvt_jpg2tiff.py b/preprocessing/cvt_jpg2tiff.py index 91a0f5b..efffdab 100644 --- a/preprocessing/cvt_jpg2tiff.py +++ b/preprocessing/cvt_jpg2tiff.py @@ -1,17 +1,7 @@ -""" -1. install convert: - https://github.com/ImageMagick/ImageMagick - -2. install tiff-tools (ubuntu 18.04): - pip install tifftools - -If you have size limit error, try to increase the limit in: /etc/ImageMagick-6/policy.xml -check: https://github.com/ImageMagick/ImageMagick/issues/396#issuecomment-319569255 - -""" import os import glob import sys +import pyvips def cvt_jpg2tiff(src_dir, dst_dir): if not os.path.exists(dst_dir): @@ -22,15 +12,10 @@ def cvt_jpg2tiff(src_dir, dst_dir): dst_file = os.path.join(dst_dir, os.path.basename(src_file).replace('.jpg', '.tiff')) # convert jpg to tiff - - cmd = f'convert {src_file} -define tiff64:tile-geometry=256x256 -compress none "ptif:{dst_file}"' - print(cmd) - os.system(cmd) - - # add metadata to tiff - cmd = 'tifftools set -y -s ImageDescription "Iscan Coreo |AppMag = 20" ' + dst_file - print(cmd) - os.system(cmd) + filename = os.path.splitext(os.path.basename(src_file))[0] + print(filename) + im = pyvips.Image.new_from_file(src_file) + im.write_to_file(dst_file, pyramid=True, tile=True, compression="none") print('Done.') if __name__ == '__main__': From a57f1aa2d2f1a5463a7a45289113483465826e42 Mon Sep 17 00:00:00 2001 From: Yuxin Wu Date: Fri, 28 Apr 2023 15:08:16 +0800 Subject: [PATCH 6/7] update for bigtiff --- preprocessing/cvt_jpg2tiff.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/preprocessing/cvt_jpg2tiff.py b/preprocessing/cvt_jpg2tiff.py index efffdab..fb6821c 100644 --- a/preprocessing/cvt_jpg2tiff.py +++ b/preprocessing/cvt_jpg2tiff.py @@ -15,7 +15,7 @@ def cvt_jpg2tiff(src_dir, dst_dir): filename = os.path.splitext(os.path.basename(src_file))[0] print(filename) im = pyvips.Image.new_from_file(src_file) - im.write_to_file(dst_file, pyramid=True, tile=True, compression="none") + im.write_to_file(dst_file, pyramid=True, tile=True, bigtiff=True, compression="none") print('Done.') if __name__ == '__main__': @@ -26,4 +26,4 @@ def cvt_jpg2tiff(src_dir, dst_dir): src_dir = args[1] dst_dir = args[2] cvt_jpg2tiff(src_dir, dst_dir) - \ No newline at end of file + From 5271e727a29062e08db49ee53183884d23153c8c Mon Sep 17 00:00:00 2001 From: Yuxin Wu Date: Mon, 8 May 2023 11:44:09 +0800 Subject: [PATCH 7/7] issue6: check bboxes of annotation --- preprocessing/cut_tumor_regions.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/preprocessing/cut_tumor_regions.py b/preprocessing/cut_tumor_regions.py index fb0d309..0a07401 100644 --- a/preprocessing/cut_tumor_regions.py +++ b/preprocessing/cut_tumor_regions.py @@ -68,6 +68,10 @@ def get_bbox(points): x, y, width, height = bbox tumour_img = wsi_img[y: y + height, x: x + width] + # pass wrong shape of tumour_img + if np.any(np.array(tumour_img.shape) == 0): + continue + # fill irrelevant areas with solid colors if args.not_filled_other_regions: mask_array = np.zeros((height, width), dtype=np.uint8)