Esempio n. 1
0
def compose_layer(layer, force=False, **kwargs):
    """Compose a single layer with pixels."""
    from PIL import Image, ImageChops
    assert layer.bbox != (0, 0, 0, 0), 'Layer bbox is (0, 0, 0, 0)'

    image = layer.topil(**kwargs)
    if image is None or force:
        texture = create_fill(layer)
        if texture is not None:
            image = texture
    if image is None:
        return image

    # TODO: Group should have the following too.

    # Apply vector mask.
    if layer.has_vector_mask() and (force or not layer.has_pixels()):
        vector_mask = draw_vector_mask(layer)
        if image.mode.endswith('A'):
            offset = vector_mask.info['offset']
            vector_mask = ImageChops.darker(image.getchannel('A'), vector_mask)
            vector_mask.info['offset'] = offset
        image.putalpha(vector_mask)

        # Apply stroke.
        if layer.has_stroke() and layer.stroke.enabled:
            image = draw_stroke(image, layer, vector_mask)

    # Apply mask.
    image = apply_mask(layer, image)

    # Apply layer fill effects.
    effect_base = image.copy()
    apply_opacity(image,
                  layer.tagged_blocks.get_data(Tag.BLEND_FILL_OPACITY, 255))
    image = apply_effect(layer, image, effect_base)

    # Clip layers.
    if layer.has_clip_layers():
        clip_box = Group.extract_bbox(layer.clip_layers)
        offset = image.info.get('offset', layer.offset)
        bbox = offset + (offset[0] + image.width, offset[1] + image.height)
        if intersect(bbox, clip_box) != (0, 0, 0, 0):
            clip_image = compose(layer.clip_layers,
                                 bbox=bbox,
                                 context=image.copy())
            if image.mode.endswith('A'):
                mask = image.getchannel('A')
            else:
                mask = Image.new('L', image.size, 255)
            if clip_image.mode.endswith('A'):
                mask = ImageChops.darker(clip_image.getchannel('A'), mask)
            clip_image.putalpha(mask)
            image = blend(image, clip_image, (0, 0))

    # Apply opacity.
    apply_opacity(image, layer.opacity)

    return image
Esempio n. 2
0
def _apply_layer_ops(layer, image, force=False, bbox=None):
    """Apply layer masks, effects, and clipping."""
    from PIL import Image, ImageChops
    # Apply vector mask.
    if layer.has_vector_mask() and (force or not layer.has_pixels()):
        offset = image.info.get('offset', layer.offset)
        mask_box = offset + (offset[0] + image.width, offset[1] + image.height)
        vector_mask = draw_vector_mask(layer, mask_box)
        if image.mode.endswith('A'):
            offset = vector_mask.info['offset']
            vector_mask = ImageChops.darker(image.getchannel('A'), vector_mask)
            vector_mask.info['offset'] = offset
        image.putalpha(vector_mask)

        # Apply stroke.
        if layer.has_stroke() and layer.stroke.enabled:
            image = draw_stroke(image, layer, vector_mask)

    # Apply mask.
    image = apply_mask(layer, image, bbox=bbox)

    # Apply layer fill effects.
    apply_opacity(
        image, layer.tagged_blocks.get_data(Tag.BLEND_FILL_OPACITY, 255)
    )
    if layer.effects.enabled:
        image = apply_effect(layer, image, image.copy())

    # Clip layers.
    if layer.has_clip_layers():
        clip_box = Group.extract_bbox(layer.clip_layers)
        offset = image.info.get('offset', layer.offset)
        bbox = offset + (offset[0] + image.width, offset[1] + image.height)
        if intersect(bbox, clip_box) != (0, 0, 0, 0):
            clip_image = compose(
                layer.clip_layers,
                force=force,
                bbox=bbox,
                context=image.copy()
            )
            if image.mode.endswith('A'):
                mask = image.getchannel('A')
            else:
                mask = Image.new('L', image.size, 255)
            if clip_image.mode.endswith('A'):
                mask = ImageChops.darker(clip_image.getchannel('A'), mask)
            clip_image.putalpha(mask)
            image = blend(image, clip_image, (0, 0))

    # Apply opacity.
    apply_opacity(image, layer.opacity)

    return image
Esempio n. 3
0
def draw_stroke(backdrop, layer, vector_mask=None):
    from PIL import Image, ImageChops
    import aggdraw
    from psd_tools.composer.blend import blend
    width = layer._psd.width
    height = layer._psd.height
    setting = layer.stroke._data

    # Draw mask.
    stroke_width = float(setting.get('strokeStyleLineWidth', 1.))
    mask = Image.new('L', (width, height))
    draw = aggdraw.Draw(mask)
    for subpath in layer.vector_mask.paths:
        path = ' '.join(map(str, _generate_symbol(subpath, width, height)))
        symbol = aggdraw.Symbol(path)
        pen = aggdraw.Pen(255, int(2 * stroke_width))
        draw.symbol((0, 0), symbol, pen, None)
    draw.flush()
    del draw

    # For now, path operations are not implemented.
    if vector_mask:
        vector_mask_ = Image.new('L', (width, height))
        vector_mask_.paste(vector_mask, vector_mask.info['offset'])
        mask = ImageChops.darker(mask, vector_mask_)

    offset = backdrop.info.get('offset', layer.offset)
    bbox = offset + (offset[0] + backdrop.width, offset[1] + backdrop.height)
    mask = mask.crop(bbox)

    # Paint the mask.
    painter = setting.get('strokeStyleContent')
    mode = setting.get('strokeStyleBlendMode').enum
    if not painter:
        logger.warning('Empty stroke style content.')
        return backdrop

    if painter.classID == b'solidColorLayer':
        image = draw_solid_color_fill(mask.size, painter)
    elif painter.classID == b'gradientLayer':
        image = draw_gradient_fill(mask.size, painter)
    elif painter.classID == b'patternLayer':
        image = draw_pattern_fill(mask.size, layer._psd, painter)
    else:
        logger.warning('Unknown painter: %s' % painter)
        return backdrop

    image.putalpha(mask)
    return blend(backdrop, image, (0, 0), mode)
Esempio n. 4
0
def compose(layers,
            bbox=None,
            context=None,
            layer_filter=None,
            color=None,
            **kwargs):
    """
    Compose layers to a single :py:class:`PIL.Image`.
    If the layers do not have visible pixels, the function returns `None`.

    Example::

        image = compose([layer1, layer2])

    In order to skip some layers, pass `layer_filter` function which
    should take `layer` as an argument and return `True` to keep the layer
    or return `False` to skip::

        image = compose(
            layers,
            layer_filter=lambda x: x.is_visible() and x.kind == 'type'
        )

    By default, visible layers are composed.

    .. note:: This function is experimental and does not guarantee
        Photoshop-quality rendering.

        Currently the following are ignored:

         - Adjustments layers
         - Layer effects
         - Blending mode (all blending modes become normal)

        Shape drawing is inaccurate if the PSD file is not saved with
        maximum compatibility.

    :param layers: a layer, or an iterable of layers.
    :param bbox: (left, top, bottom, right) tuple that specifies a region to
        compose. By default, all the visible area is composed. The origin
        is at the top-left corner of the PSD document.
    :param context: `PIL.Image` object for the backdrop rendering context. Must
        be used with the correct `bbox` size.
    :param layer_filter: a callable that takes a layer and returns `bool`.
    :param color: background color in `int` or `tuple`.
    :return: :py:class:`PIL.Image` or `None`.
    """
    from PIL import Image

    if not hasattr(layers, '__iter__'):
        layers = [layers]

    def _default_filter(layer):
        return layer.is_visible()

    layer_filter = layer_filter or _default_filter
    valid_layers = [x for x in layers if layer_filter(x)]
    if len(valid_layers) == 0:
        return context

    if bbox is None:
        bbox = Group.extract_bbox(valid_layers)
        if bbox == (0, 0, 0, 0):
            return context

    if context is None:
        mode = get_pil_mode(valid_layers[0]._psd.color_mode, True)
        context = Image.new(
            mode,
            (bbox[2] - bbox[0], bbox[3] - bbox[1]),
            color=color if color is not None else 'white',
        )
        context.putalpha(0)  # Alpha must be forced to correctly blend.
        context.info['offset'] = (bbox[0], bbox[1])

    for layer in valid_layers:
        if intersect(layer.bbox, bbox) == (0, 0, 0, 0):
            continue

        if layer.is_group():
            if layer.blend_mode == BlendMode.PASS_THROUGH:
                context = layer.compose(context=context, bbox=bbox, **kwargs)
                continue
            else:
                image = layer.compose(**kwargs)
        else:
            image = compose_layer(layer, **kwargs)
        if image is None:
            continue

        logger.debug('Composing %s' % layer)
        offset = image.info.get('offset', layer.offset)
        offset = (offset[0] - bbox[0], offset[1] - bbox[1])

        context = blend(context, image, offset, layer.blend_mode)

    return context
Esempio n. 5
0
def apply_effect(layer, backdrop, base_image):
    """Apply effect to the image.

    ..note: Correct effect order is the following. All the effects are first
        applied to the original image then blended together.

        * dropshadow
        * outerglow
        * (original)
        * patternoverlay
        * gradientoverlay
        * coloroverlay
        * innershadow
        * innerglow
        * bevelemboss
        * satin
        * stroke
    """
    from PIL import ImageChops
    for effect in layer.effects:
        if effect.__class__.__name__ == 'PatternOverlay':
            image = draw_pattern_fill(base_image.size, layer._psd,
                                      effect.value)
            if base_image.mode.endswith('A'):
                alpha = base_image.getchannel('A')
                if image.mode.endswith('A'):
                    alpha = ImageChops.darker(alpha, image.getchannel('A'))
                image.putalpha(alpha)
            backdrop = blend(backdrop, image, (0, 0), effect.blend_mode)

    for effect in layer.effects:
        if effect.__class__.__name__ == 'GradientOverlay':
            image = draw_gradient_fill(base_image.size, effect.value)
            if base_image.mode.endswith('A'):
                alpha = base_image.getchannel('A')
                if image.mode.endswith('A'):
                    alpha = ImageChops.darker(alpha, image.getchannel('A'))
                image.putalpha(alpha)
            backdrop = blend(backdrop, image, (0, 0), effect.blend_mode)

    for effect in layer.effects:
        if effect.__class__.__name__ == 'ColorOverlay':
            image = draw_solid_color_fill(base_image.size, effect.value)
            if base_image.mode.endswith('A'):
                alpha = base_image.getchannel('A')
                if image.mode.endswith('A'):
                    alpha = ImageChops.darker(alpha, image.getchannel('A'))
                image.putalpha(alpha)
            backdrop = blend(backdrop, image, (0, 0), effect.blend_mode)

    for effect in layer.effects:
        if effect.__class__.__name__ == 'Stroke':
            from PIL import ImageOps

            if layer.has_vector_mask():
                alpha = draw_vector_mask(layer)
            elif base_image.mode.endswith('A'):
                alpha = base_image.getchannel('A')
            else:
                alpha = base_image.convert('L')
            alpha.info['offset'] = base_image.info['offset']
            flat = alpha.getextrema()[0] < 255

            # Expand the image size
            setting = effect.value
            size = int(setting.get(Key.SizeKey))
            offset = backdrop.info['offset']
            backdrop = ImageOps.expand(backdrop, size)
            backdrop.info['offset'] = tuple(x - size for x in offset)
            offset = alpha.info['offset']
            alpha = ImageOps.expand(alpha, size)
            alpha.info['offset'] = tuple(x - size for x in offset)

            if not layer.has_vector_mask() and setting.get(
                    Key.Style).enum == Enum.InsetFrame and flat:
                image = create_stroke_effect(alpha, setting, layer._psd, True)
                backdrop.paste(image)
            else:
                image = create_stroke_effect(alpha, setting, layer._psd)
                backdrop = blend(backdrop, image, (0, 0), effect.blend_mode)

    return backdrop