Ejemplo n.º 1
0
def run(args):
    import os
    import libtbx.load_env
    from libtbx.utils import Sorry
    from dials.util.options import OptionParser
    usage = "%s [options] datablock.json | image.cbf" % libtbx.env.dispatcher_name

    parser = OptionParser(usage=usage,
                          phil=phil_scope,
                          read_datablocks=True,
                          read_datablocks_from_images=True,
                          check_format=True,
                          epilog=help_message)

    params, options = parser.parse_args(show_diff_phil=True)

    datablocks = flatten_datablocks(params.input.datablock)
    if len(datablocks) == 0:
        parser.print_help()
        exit(0)

    imagesets = datablocks[0].extract_imagesets()

    brightness = params.brightness / 100
    vendortype = "made up"

    # check that binning is a power of 2
    binning = params.binning
    if not (binning > 0 and ((binning & (binning - 1)) == 0)):
        raise Sorry("binning must be a power of 2")

    output_dir = params.output_dir
    if output_dir is None:
        output_dir = "."
    elif not os.path.exists(output_dir):
        os.makedirs(output_dir)

    from rstbx.slip_viewer.tile_generation \
         import _get_flex_image, _get_flex_image_multipanel

    for imageset in imagesets:
        detector = imageset.get_detector()

        if len(detector) > 1:
            raise Sorry(
                'Currently only single panel detectors are supported by %s' %
                libtbx.env.dispatcher_name)

        panel = detector[0]
        scan = imageset.get_scan()
        # XXX is this inclusive or exclusive?
        saturation = panel.get_trusted_range()[1]
        if params.saturation:
            saturation = params.saturation
        if scan is not None and scan.get_oscillation()[1] > 0:
            start, end = scan.get_image_range()
        else:
            start, end = 0, len(imageset)
        for i_image in range(start, end + 1):
            image = imageset.get_raw_data(i_image - start)

            #if len(detector) == 1:
            #image = [image]

            trange = [p.get_trusted_range() for p in detector]
            mask = imageset.get_mask(i_image - start)
            if mask is None:
                mask = [
                    p.get_trusted_range_mask(im)
                    for im, p in zip(image, detector)
                ]

            if params.show_mask:
                for rd, m in zip(image, mask):
                    rd.set_selected(~m, -2)

            image = image_filter(image,
                                 mask,
                                 display=params.display,
                                 gain_value=params.gain,
                                 nsigma_b=params.nsigma_b,
                                 nsigma_s=params.nsigma_s,
                                 global_threshold=params.global_threshold,
                                 min_local=params.min_local,
                                 kernel_size=params.kernel_size)

            show_untrusted = params.show_mask
            if len(detector) > 1:
                # FIXME This doesn't work properly, as flex_image.size2() is incorrect
                # also binning doesn't work
                assert binning == 1
                flex_image = _get_flex_image_multipanel(
                    brightness=brightness,
                    panels=detector,
                    raw_data=image,
                    beam=imageset.get_beam(),
                    show_untrusted=show_untrusted)
            else:
                flex_image = _get_flex_image(brightness=brightness,
                                             data=image[0],
                                             binning=binning,
                                             saturation=saturation,
                                             vendortype=vendortype,
                                             show_untrusted=show_untrusted)

            flex_image.setWindow(0, 0, 1)
            flex_image.adjust(
                color_scheme=colour_schemes.get(params.colour_scheme))

            # now export as a bitmap
            flex_image.prep_string()
            try:
                from PIL import Image
            except ImportError:
                import Image
            # XXX is size//binning safe here?
            try:  # fromstring raises Exception in Pillow >= 3.0.0
                pil_img = Image.fromstring('RGB',
                                           (flex_image.size2() // binning,
                                            flex_image.size1() // binning),
                                           flex_image.export_string)
            except Exception:
                pil_img = Image.frombytes('RGB',
                                          (flex_image.size2() // binning,
                                           flex_image.size1() // binning),
                                          flex_image.export_string)
            path = os.path.join(
                output_dir,
                params.prefix + ("%04d" % i_image) + '.' + params.format)

            print "Exporting %s" % path
            with open(path, 'wb') as tmp_stream:
                pil_img.save(tmp_stream,
                             format=params.format,
                             compress_level=params.png.compress_level,
                             quality=params.jpeg.quality)
Ejemplo n.º 2
0
def run(args):
  import os
  import libtbx.load_env
  from libtbx.utils import Sorry
  from dials.util.options import OptionParser
  usage = "%s [options] datablock.json | image.cbf" %libtbx.env.dispatcher_name

  parser = OptionParser(
    usage=usage,
    phil=phil_scope,
    read_datablocks=True,
    read_datablocks_from_images=True,
    check_format=True,
    epilog=help_message)

  params, options = parser.parse_args(show_diff_phil=True)

  datablocks = flatten_datablocks(params.input.datablock)
  if len(datablocks) == 0:
    parser.print_help()
    exit(0)

  imagesets = datablocks[0].extract_imagesets()

  brightness = params.brightness / 100
  vendortype = "made up"

  # check that binning is a power of 2
  binning = params.binning
  if not (binning > 0 and ((binning & (binning - 1)) == 0)):
    raise Sorry("binning must be a power of 2")

  output_dir = params.output_dir
  if output_dir is None:
    output_dir = "."
  elif not os.path.exists(output_dir):
    os.makedirs(output_dir)

  from rstbx.slip_viewer.tile_generation \
       import _get_flex_image, _get_flex_image_multipanel

  for imageset in imagesets:
    detector = imageset.get_detector()
    panel = detector[0]
    # XXX is this inclusive or exclusive?
    saturation = panel.get_trusted_range()[1]
    if params.saturation:
      saturation = params.saturation
    for i_image, image in enumerate(imageset):

      if len(detector) == 1:
        image = [image]

      trange = [p.get_trusted_range() for p in detector]
      mask = []
      mask = imageset.get_mask(i_image)
      if mask is None:
        mask = [p.get_trusted_range_mask(im) for im, p in zip(image, detector)]

      image = image_filter(image, mask, display=params.display, gain_value=params.gain,
                           nsigma_b=params.nsigma_b,
                           nsigma_s=params.nsigma_s,
                           global_threshold=params.global_threshold,
                           min_local=params.min_local,
                           kernel_size=params.kernel_size)

      if len(detector) > 1:
        # FIXME This doesn't work properly, as flex_image.size2() is incorrect
        # also binning doesn't work
        assert binning == 1
        flex_image = _get_flex_image_multipanel(
          brightness=brightness,
          panels=detector,
          raw_data=image)
      else:
        flex_image = _get_flex_image(
          brightness=brightness,
          data=image[0],
          binning=binning,
          saturation=saturation,
          vendortype=vendortype)

      flex_image.setWindow(0, 0, 1)
      flex_image.adjust(color_scheme=colour_schemes.get(params.colour_scheme))

      # now export as a bitmap
      flex_image.prep_string()
      import Image
      # XXX is size//binning safe here?
      pil_img = Image.fromstring(
        'RGB', (flex_image.size2()//binning,
                flex_image.size1()//binning),
        flex_image.export_string)

      path = os.path.join(
        output_dir, params.prefix + ("%04d" % i_image) + '.' + params.format)

      print "Exporting %s" %path
      tmp_stream = open(path, 'wb')
      pil_img.save(tmp_stream, format=params.format)
      tmp_stream.close()
Ejemplo n.º 3
0
def run(args):
    import os
    from libtbx.phil import command_line
    from libtbx.utils import Sorry, Usage

    if len(args) == 0:
        from cStringIO import StringIO
        s = StringIO()
        master_phil_scope.show(out=s)
        raise Usage("""\
dxtbx.export_bitmaps image_files [options]

% s
""" % s.getvalue())

    from dxtbx.datablock import DataBlockFactory
    unhandled = []
    datablocks = DataBlockFactory.from_args(args,
                                            verbose=False,
                                            unhandled=unhandled)
    assert len(datablocks) > 0
    imagesets = datablocks[0].extract_imagesets()

    cmd_line = command_line.argument_interpreter(
        master_params=master_phil_scope)
    working_phil = cmd_line.process_and_fetch(args=unhandled)
    working_phil.show()
    params = working_phil.extract()

    brightness = params.brightness / 100
    vendortype = "made up"

    # check that binning is a power of 2
    binning = params.binning
    if not (binning > 0 and ((binning & (binning - 1)) == 0)):
        raise Sorry("binning must be a power of 2")

    output_dir = params.output_dir
    if output_dir is None:
        output_dir = "."
    elif not os.path.exists(output_dir):
        os.makedirs(output_dir)

    from rstbx.slip_viewer.tile_generation \
         import _get_flex_image, _get_flex_image_multipanel

    for imageset in imagesets:
        detector = imageset.get_detector()
        panel = detector[0]
        # XXX is this inclusive or exclusive?
        saturation = panel.get_trusted_range()[1]
        for i_image, image in enumerate(imageset):

            if len(detector) > 1:
                # FIXME This doesn't work properly, as flex_image.size2() is incorrect
                # also binning doesn't work
                assert binning == 1
                flex_image = _get_flex_image_multipanel(
                    brightness=brightness,
                    panels=detector,
                    raw_data=image,
                    beam=imageset.get_beam())
            else:
                flex_image = _get_flex_image(brightness=brightness,
                                             data=image,
                                             binning=binning,
                                             saturation=saturation,
                                             vendortype=vendortype)

            flex_image.setWindow(0, 0, 1)
            flex_image.adjust(
                color_scheme=colour_schemes.get(params.colour_scheme))

            # now export as a bitmap
            flex_image.prep_string()
            try:
                import PIL.Image as Image
            except ImportError:
                import Image
            # XXX is size//binning safe here?
            try:
                pil_img = Image.fromstring('RGB',
                                           (flex_image.size2() // binning,
                                            flex_image.size1() // binning),
                                           flex_image.export_string)
            except NotImplementedError:
                pil_img = Image.frombytes('RGB',
                                          (flex_image.size2() // binning,
                                           flex_image.size1() // binning),
                                          flex_image.export_string)

            basename = os.path.basename(
                os.path.splitext(imageset.paths()[i_image])[0])
            path = os.path.join(output_dir, basename + '.' + params.format)

            print "Exporting %s" % path
            tmp_stream = open(path, 'wb')
            pil_img.save(tmp_stream, format=params.format)
            tmp_stream.close()
Ejemplo n.º 4
0
def imageset_as_bitmaps(imageset, params):
    from rstbx.slip_viewer.tile_generation import (
        _get_flex_image,
        _get_flex_image_multipanel,
    )

    brightness = params.brightness / 100
    vendortype = "made up"
    # check that binning is a power of 2
    binning = params.binning
    if not (binning > 0 and ((binning & (binning - 1)) == 0)):
        raise Sorry("binning must be a power of 2")
    output_dir = params.output_dir
    if output_dir is None:
        output_dir = "."
    elif not os.path.exists(output_dir):
        os.makedirs(output_dir)
    output_files = []

    detector = imageset.get_detector()

    panel = detector[0]
    scan = imageset.get_scan()
    # XXX is this inclusive or exclusive?
    saturation = panel.get_trusted_range()[1]
    if params.saturation:
        saturation = params.saturation
    if scan is not None and scan.get_oscillation(
    )[1] > 0 and not params.imageset_index:
        start, end = scan.get_image_range()
    else:
        start, end = 1, len(imageset)
    # If the user specified an image range index, only export those
    image_range = [
        i for i in range(start, end + 1)
        if not params.imageset_index or i in params.imageset_index
    ]
    if params.output_file and len(image_range) != 1:
        sys.exit(
            "output_file can only be specified if a single image is exported")
    for i_image in image_range:
        image = imageset.get_raw_data(i_image - start)

        mask = imageset.get_mask(i_image - start)
        if mask is None:
            mask = [
                p.get_trusted_range_mask(im) for im, p in zip(image, detector)
            ]

        if params.show_mask:
            for rd, m in zip(image, mask):
                rd.set_selected(~m, -2)

        image = image_filter(
            image,
            mask,
            display=params.display,
            gain_value=params.gain,
            nsigma_b=params.nsigma_b,
            nsigma_s=params.nsigma_s,
            global_threshold=params.global_threshold,
            min_local=params.min_local,
            kernel_size=params.kernel_size,
        )

        show_untrusted = params.show_mask
        if len(detector) > 1:
            # FIXME This doesn't work properly, as flex_image.size2() is incorrect
            # also binning doesn't work
            flex_image = _get_flex_image_multipanel(
                brightness=brightness,
                panels=detector,
                raw_data=image,
                binning=binning,
                beam=imageset.get_beam(),
                show_untrusted=show_untrusted,
            )
        else:
            flex_image = _get_flex_image(
                brightness=brightness,
                data=image[0],
                binning=binning,
                saturation=saturation,
                vendortype=vendortype,
                show_untrusted=show_untrusted,
            )

        flex_image.setWindow(0, 0, 1)
        flex_image.adjust(
            color_scheme=colour_schemes.get(params.colour_scheme))

        # now export as a bitmap
        flex_image.prep_string()
        from PIL import Image

        # XXX is size//binning safe here?
        pil_img = Image.frombytes(
            "RGB", (flex_image.ex_size2(), flex_image.ex_size1()),
            flex_image.as_bytes())
        if params.output_file:
            path = os.path.join(output_dir, params.output_file)
        else:
            path = os.path.join(
                output_dir,
                "{p.prefix}{image:0{p.padding}}.{p.format}".format(
                    p=params, image=i_image),
            )

        print("Exporting %s" % path)
        output_files.append(path)
        with open(path, "wb") as tmp_stream:
            pil_img.save(
                tmp_stream,
                format=params.format,
                compress_level=params.png.compress_level,
                quality=params.jpeg.quality,
            )

    return output_files
Ejemplo n.º 5
0
def run(args):
  import os
  from libtbx.phil import command_line
  from libtbx.utils import Sorry, Usage

  if len(args) == 0:
    from cStringIO import StringIO
    s = StringIO()
    master_phil_scope.show(out=s)
    raise Usage("""\
dxtbx.export_bitmaps image_files [options]

% s
""" %s.getvalue())

  from dxtbx.datablock import DataBlockFactory
  unhandled = []
  datablocks = DataBlockFactory.from_args(
    args, verbose=False, unhandled=unhandled)
  assert len(datablocks) > 0
  imagesets = datablocks[0].extract_imagesets()

  cmd_line = command_line.argument_interpreter(master_params=master_phil_scope)
  working_phil = cmd_line.process_and_fetch(args=unhandled)
  working_phil.show()
  params = working_phil.extract()

  brightness = params.brightness / 100
  vendortype = "made up"

  # check that binning is a power of 2
  binning = params.binning
  if not (binning > 0 and ((binning & (binning - 1)) == 0)):
    raise Sorry("binning must be a power of 2")

  output_dir = params.output_dir
  if output_dir is None:
    output_dir = "."
  elif not os.path.exists(output_dir):
    os.makedirs(output_dir)

  from rstbx.slip_viewer.tile_generation \
       import _get_flex_image, _get_flex_image_multipanel

  for imageset in imagesets:
    detector = imageset.get_detector()
    panel = detector[0]
    # XXX is this inclusive or exclusive?
    saturation = panel.get_trusted_range()[1]
    for i_image, image in enumerate(imageset):

      if len(detector) > 1:
        # FIXME This doesn't work properly, as flex_image.size2() is incorrect
        # also binning doesn't work
        assert binning == 1
        flex_image = _get_flex_image_multipanel(
          brightness=brightness,
          panels=detector,
          raw_data=image)
      else:
        flex_image = _get_flex_image(
          brightness=brightness,
          data=image,
          binning=binning,
          saturation=saturation,
          vendortype=vendortype)

      flex_image.setWindow(0, 0, 1)
      flex_image.adjust(color_scheme=colour_schemes.get(params.colour_scheme))

      # now export as a bitmap
      flex_image.prep_string()
      import Image
      # XXX is size//binning safe here?
      pil_img = Image.fromstring(
        'RGB', (flex_image.size2()//binning,
                flex_image.size1()//binning),
        flex_image.export_string)

      basename = os.path.basename(os.path.splitext(imageset.paths()[i_image])[0])
      path = os.path.join(
        output_dir, basename + '.' + params.format)

      print "Exporting %s" %path
      tmp_stream = open(path, 'wb')
      pil_img.save(tmp_stream, format=params.format)
      tmp_stream.close()