Example #1
0
    def create_pattern(self, setting, insert=(0, 0)):
        """Create a pattern element."""
        pattern_id = setting['Ptrn']['Idnt'].value.strip('\x00')
        pattern = self._psd._get_pattern(pattern_id)
        phase = (0, 0)  #setting.phase
        scale = 100.  #setting.scale.value  # TODO: Check unit
        if not pattern:
            logger.error('Pattern data not found')
            return self._dwg.defs.add(svgwrite.pattern.Pattern())

        image = convert_pattern_to_pil(pattern)
        element = self._dwg.defs.add(svgwrite.pattern.Pattern(
            width=image.width,
            height=image.height,
            patternUnits='userSpaceOnUse',
            patternContentUnits='userSpaceOnUse',
            patternTransform='translate({},{}) scale({})'.format(
                insert[0] + phase[0], insert[1] + phase[1], scale / 100.0),
        ))
        element.add(self._dwg.image(
            self._get_image_href(image),
            insert=(0, 0),
            size=(image.width, image.height),
        ))
        return element
Example #2
0
def draw_pattern_fill(size, psd, setting):
    """
    Create a pattern fill image.

    :param size: (width, height) tuple.
    :param psd: :py:class:`PSDImage`.
    :param setting: Descriptor containing pattern fill.
    """
    from PIL import Image
    pattern_id = setting[Enum.Pattern][Key.ID].value.rstrip('\x00')
    pattern = psd._get_pattern(pattern_id)
    if not pattern:
        logger.error('Pattern not found: %s' % (pattern_id))
        return None
    panel = convert_pattern_to_pil(pattern)

    scale = float(setting.get(Key.Scale, 100.)) / 100.
    if scale != 1.:
        panel = panel.resize((
            max(1, int(panel.width * scale)),
            max(1, int(panel.height * scale)),
        ))
    _apply_opacity(panel, setting)

    pattern_image = Image.new(panel.mode, size)
    for top in range(0, pattern_image.height, panel.height):
        for left in range(0, pattern_image.width, panel.width):
            pattern_image.paste(panel, (left, top))

    return pattern_image
Example #3
0
def draw_pattern_fill(image, psd, setting):
    pattern_id = setting[b'Ptrn'][b'Idnt'].value.rstrip('\x00')
    pattern = psd._get_pattern(pattern_id)
    if not pattern:
        logger.error('Pattern not found: %s' % (pattern_id))
        return None
    panel = convert_pattern_to_pil(pattern, psd._record.header.version)
    for left in range(0, image.width, panel.width):
        for top in range(0, image.height, panel.height):
            image.paste(panel, (left, top))
Example #4
0
def draw_pattern_fill(image, psd, setting, blend=True):
    """
    Draw pattern fill on the image.

    :param image: Image to be filled.
    :param psd: :py:class:`PSDImage`.
    :param setting: Descriptor containing pattern fill.
    :param blend: Blend the fill or ignore. Effects blend.
    """
    from PIL import Image
    pattern_id = setting[b'Ptrn'][b'Idnt'].value.rstrip('\x00')
    pattern = psd._get_pattern(pattern_id)
    if not pattern:
        logger.error('Pattern not found: %s' % (pattern_id))
        return None
    panel = convert_pattern_to_pil(pattern, psd._record.header.version)

    scale = setting.get(b'Scl ', 100) / 100.
    if scale != 1.:
        panel = panel.resize(
            (int(panel.width * scale), int(panel.height * scale)))

    opacity = int(setting.get(b'Opct', 100) / 100. * 255)
    if opacity != 255:
        panel.putalpha(opacity)

    pattern_image = Image.new(image.mode, image.size)
    mask = image.getchannel('A') if blend else Image.new('L', image.size, 255)

    for left in range(0, pattern_image.width, panel.width):
        for top in range(0, pattern_image.height, panel.height):
            panel_mask = mask.crop(
                (left, top, left + panel.width, top + panel.height))
            pattern_image.paste(panel, (left, top), panel_mask)

    if blend:
        image.paste(_blend(image, pattern_image, (0, 0)))
    else:
        image.paste(pattern_image)
Example #5
0
def test_convert_pattern_to_pil():
    filepath = os.path.join(TEST_ROOT, 'tagged_blocks', 'Patt_1.dat')
    with open(filepath, 'rb') as f:
        pattern = Pattern.read(f)

    assert pil_io.convert_pattern_to_pil(pattern)