Exemplo n.º 1
0
    def merge(self, sources, image_opts, size=None, bbox=None, bbox_srs=None, coverage=None):
        if not sources:
            return BlankImageSource(size=size, image_opts=image_opts, cacheable=True)

        if size is None:
            size = sources[0].size

        # load src bands
        src_img_bands = []
        for i, layer_img in enumerate(sources):
            img = layer_img.as_image()

            if i not in self.max_band:
                # do not split img if not requested by any op
                src_img_bands.append(None)
                continue

            if self.max_band[i] == 3 and img.mode != 'RGBA':
                # convert to RGBA if band idx 3 is requestd (e.g. P or RGB src)
                img = img.convert('RGBA')
            elif img.mode == 'P':
                img = img.convert('RGB')
            src_img_bands.append(img.split())

        tmp_mode = self.mode

        if tmp_mode == 'RGBA':
            result_bands = [None, None, None, None]
        elif tmp_mode == 'RGB':
            result_bands = [None, None, None]
        elif tmp_mode == 'L':
            result_bands = [None]
        else:
            raise ValueError("unsupported destination mode %s", image_opts.mode)

        for op in self.ops:
            chan = src_img_bands[op.src_img][op.src_band]
            if op.factor != 1.0:
                chan = ImageMath.eval("convert(int(float(a) * %f), 'L')" % op.factor, a=chan)
                if result_bands[op.dst_band] is None:
                    result_bands[op.dst_band] = chan
                else:
                    result_bands[op.dst_band] = ImageChops.add(
                        result_bands[op.dst_band],
                        chan,
                    )
            else:
                result_bands[op.dst_band] = chan

        for i, b in enumerate(result_bands):
            if b is None:
                # band not set
                b = Image.new("L", size, 255 if i == 3 else 0)
                result_bands[i] = b

        result = Image.merge(tmp_mode, result_bands)
        return ImageSource(result, size=size, image_opts=image_opts)
Exemplo n.º 2
0
    def merge(self,
              sources,
              image_opts,
              size=None,
              bbox=None,
              bbox_srs=None,
              coverage=None):
        if len(sources) < self.max_src_images:
            return BlankImageSource(size=size,
                                    image_opts=image_opts,
                                    cacheable=True)

        if size is None:
            size = sources[0].size

        # load src bands
        src_img_bands = []
        for i, layer_img in enumerate(sources):
            img = layer_img.as_image()

            if i not in self.max_band:
                # do not split img if not requested by any op
                src_img_bands.append(None)
                continue

            if self.max_band[i] == 3 and img.mode != 'RGBA':
                # convert to RGBA if band idx 3 is requestd (e.g. P or RGB src)
                img = img.convert('RGBA')
            elif img.mode == 'P':
                img = img.convert('RGB')
            src_img_bands.append(img.split())

        tmp_mode = self.mode

        if tmp_mode == 'RGBA':
            result_bands = [None, None, None, None]
        elif tmp_mode == 'RGB':
            result_bands = [None, None, None]
        elif tmp_mode == 'L':
            result_bands = [None]
        else:
            raise ValueError("unsupported destination mode %s",
                             image_opts.mode)

        for op in self.ops:
            chan = src_img_bands[op.src_img][op.src_band]
            if op.factor != 1.0:
                chan = ImageMath.eval("convert(int(float(a) * %f), 'L')" %
                                      op.factor,
                                      a=chan)
                if result_bands[op.dst_band] is None:
                    result_bands[op.dst_band] = chan
                else:
                    result_bands[op.dst_band] = ImageChops.add(
                        result_bands[op.dst_band],
                        chan,
                    )
            else:
                result_bands[op.dst_band] = chan

        for i, b in enumerate(result_bands):
            if b is None:
                # band not set
                b = Image.new("L", size, 255 if i == 3 else 0)
                result_bands[i] = b

        result = Image.merge(tmp_mode, result_bands)
        return ImageSource(result, size=size, image_opts=image_opts)