Created
January 22, 2020 02:49
-
-
Save Yu-AnChen/031bb6aedaac4a47428abdd9793d0e84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytiff | |
import skimage.io | |
try: | |
import pathlib | |
except ImportError: | |
import pathlib2 as pathlib | |
import argparse | |
import warnings | |
import sys | |
parser = argparse.ArgumentParser( | |
description='Crop a pyramidal OME-TIFF with supplied starting and ending row and starting and ending column' | |
) | |
parser.add_argument( | |
'input_filepath', metavar='ASHLAR_OMETIFF', | |
help='A OME-TIFF generated by ashlar' | |
) | |
parser.add_argument( | |
'start_row', metavar='START_ROW', | |
help='Starting row, starts from zero, this row is included' | |
) | |
parser.add_argument( | |
'end_row', metavar='END_ROW', | |
help='Ending row, this row is not included' | |
) | |
parser.add_argument( | |
'start_column', metavar='START_COLUMN', | |
help='Starting column, starts from zero, this column is included' | |
) | |
parser.add_argument( | |
'end_column', metavar='END_COLUMN', | |
help='Ending column, this column is not included' | |
) | |
parser.add_argument( | |
'output_filepath', metavar='OUTPUT_FILEPATH', | |
help='Conmplete output filepath, must ends with .ome.tif' | |
) | |
args = parser.parse_args() | |
input_filepath = pathlib.Path(args.input_filepath) | |
output_filepath = pathlib.Path(args.output_filepath) | |
if output_filepath.exists: | |
output_filepath.unlink() | |
tiff = pytiff.Tiff(str(input_filepath)) | |
print('{} -> {}'.format(input_filepath.name, output_filepath.name)) | |
page = 0 | |
tiff.set_page(page) | |
size = tiff.size | |
while tiff.size == size: | |
cropped = tiff[ | |
int(args.start_row):int(args.end_row), | |
int(args.start_column):int(args.end_column) | |
] | |
sys.stdout.write('\rsaving page {}'.format(page)) | |
sys.stdout.flush() | |
with warnings.catch_warnings(): | |
warnings.filterwarnings( | |
'ignore', r'.* is a low contrast image', UserWarning, | |
'^skimage\.io' | |
) | |
skimage.io.imsave( | |
str(output_filepath), cropped, bigtiff=True, | |
photometric='minisblack', metadata=None, append=True | |
) | |
page += 1 | |
tiff.set_page(page) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment