Skip to content

Commit

Permalink
Merge pull request #663 from girder/converter-associated-image-options
Browse files Browse the repository at this point in the history
Allow excluding associated images from the conversion
  • Loading branch information
manthey authored Oct 1, 2021
2 parents eaf7f80 + 42dffc6 commit 9cc678f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
25 changes: 24 additions & 1 deletion utilities/converter/large_image_converter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import math
import os
import re
import struct
import threading
import time
Expand Down Expand Up @@ -40,6 +41,25 @@
FrameMemoryEstimate = 3 * 1024 ** 3


def _use_associated_image(key, **kwargs):
"""
Check if an associated image key should be used. If a list of images to
keep was specified, it must match at least one of the regex in that list.
If a list of images to exclude was specified, it must not any regex in that
list. The exclude list takes priority.
"""
if kwargs.get('_exclude_associated'):
for exp in kwargs['_exclude_associated']:
if re.match(exp, key):
return False
if kwargs.get('_keep_associated'):
for exp in kwargs['_keep_associated']:
if re.match(exp, key):
return True
return False
return True


def _data_from_large_image(path, outputPath, **kwargs):
"""
Check if the input file can be read by installed large_image tile sources.
Expand Down Expand Up @@ -75,6 +95,8 @@ def _data_from_large_image(path, outputPath, **kwargs):
tasks = []
pool = _get_thread_pool(**kwargs)
for key in ts.getAssociatedImagesList():
if not _use_associated_image(key, **kwargs):
continue
try:
img, mime = ts.getAssociatedImage(key)
except Exception:
Expand Down Expand Up @@ -185,6 +207,8 @@ def _generate_multiframe_tiff(inputPath, outputPath, tempPath, lidata, **kwargs)
for w, h, subInputPath, page in imageSizes:
if (w, h) not in possibleSizes:
key = 'image_%d' % page
if not _use_associated_image(key, **kwargs):
continue
savePath = tempPath + '-%s-%s.tiff' % (key, time.strftime('%Y%m%d-%H%M%S'))
_pool_add(tasks, (pool.submit(
_convert_via_vips, subInputPath, savePath, tempPath, False), ))
Expand Down Expand Up @@ -601,7 +625,6 @@ def _output_tiff(inputs, outputPath, tempPath, lidata, extraImages=None, **kwarg
if extraImages:
assocList += list(extraImages.items())
for key, assocPath in assocList:
logger.debug('Reading %s', assocPath)
assocInfo = tifftools.read_tiff(assocPath)
assocInfo['ifds'][0]['tags'][tifftools.Tag.ImageDescription.value] = {
'data': key,
Expand Down
9 changes: 9 additions & 0 deletions utilities/converter/large_image_converter/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ def get_parser():
help='JP2K peak signal to noise ratio. 0 for lossless.')
parser.add_argument(
'--cr', type=int, help='JP2K compression ratio. 1 for lossless.')
parser.add_argument(
'--only-associated', dest='_keep_associated', action='append',
help='Only keep associated images with the specified keys. The value '
'is used as a matching regex.')
parser.add_argument(
'--exclude-associated', dest='_exclude_associated', action='append',
help='Exclude associated images with the specified keys. The value '
'is used as a matching regex. If a key is specified for both '
'exclusion and inclusion, it will be excluded.')
parser.add_argument(
'--concurrency', '-j', type=int, dest='_concurrency',
help='Maximum processor concurrency. Some conversion tasks can use '
Expand Down

0 comments on commit 9cc678f

Please sign in to comment.