Beispiel #1
0
    def __init__(self, decoded_data):
        self.header = decoded_data.header
        self.decoded_data = decoded_data

        # wrap decoded data to Layer and Group structures
        def fill_group(group, data):

            for layer in data['layers']:
                index = layer['index']

                if 'layers' in layer:
                    # group
                    sub_group = Group(group, index, [])
                    fill_group(sub_group, layer)
                    group._add_layer(sub_group)
                else:
                    # regular layer
                    group._add_layer(Layer(group, index))

        self._psd = self
        fake_root_data = {'layers': group_layers(decoded_data), 'index': None}
        root = _RootGroup(self, None, [])
        fill_group(root, fake_root_data)

        self._fake_root_group = root
        self.layers = root.layers
        self.embedded = [
            Embedded(linked) for linked in self._linked_layer_iter()
        ]
        self.embedded_images = [
            PSDImage.from_stream(io.BytesIO(e.data)) for e in self.embedded
        ]

        self.layer_comps = _parse_layer_comps(
            decoded_data.image_resource_blocks)
Beispiel #2
0
    def __init__(self, decoded_data):
        self.header = decoded_data.header
        self.decoded_data = decoded_data

        # wrap decoded data to Layer and Group structures
        def fill_group(group, data):

            for layer in data["layers"]:
                index = layer["index"]

                if "layers" in layer:
                    # group
                    sub_group = Group(group, index, [])
                    fill_group(sub_group, layer)
                    group._add_layer(sub_group)
                else:
                    # regular layer
                    group._add_layer(Layer(group, index))

        self._psd = self
        fake_root_data = {"layers": group_layers(decoded_data), "index": None}
        root = _RootGroup(self, None, [])
        fill_group(root, fake_root_data)

        self._fake_root_group = root
        self.layers = root.layers
        self.embedded = [Embedded(linked) for linked in self._linked_layer_iter()]
        self.embedded_images = [PSDImage.from_stream(io.BytesIO(e.data)) for e in self.embedded]

        self.layer_comps = _parse_layer_comps(decoded_data.image_resource_blocks)
Beispiel #3
0
    def __init__(self, decoded_data):
        self.header = decoded_data.header
        self.decoded_data = decoded_data

        # wrap decoded data to Layer and Group structures
        def fill_group(group, data):

            for layer in data['layers']:
                index = layer['index']

                if 'layers' in layer:
                    # group
                    sub_group = Group(group, index, [])
                    fill_group(sub_group, layer)
                    group._add_layer(sub_group)
                else:
                    # regular layer
                    group._add_layer(Layer(group, index))

        self._psd = self
        fake_root_data = {'layers': group_layers(decoded_data), 'index': None}
        root = _RootGroup(self, None, [])
        fill_group(root, fake_root_data)

        self._fake_root_group = root
        self.layers = root.layers
Beispiel #4
0
    def __init__(self, decoded_data):
        self.header = decoded_data.header
        self.decoded_data = decoded_data

        # wrap decoded data to Layer and Group structures
        def fill_group(group, data):

            for layer in data['layers']:
                index = layer['index']

                if 'layers' in layer:
                    # group
                    sub_group = Group(group, index, [])
                    fill_group(sub_group, layer)
                    group._add_layer(sub_group)
                else:
                    # regular layer
                    group._add_layer(Layer(group, index))

        self._psd = self
        fake_root_data = {'layers': group_layers(decoded_data), 'index': None}
        root = _RootGroup(self, None, [])
        fill_group(root, fake_root_data)

        self._fake_root_group = root
        self.layers = root.layers
Beispiel #5
0
def test_layer_visibility():
    visible = dict(
        (layer['name'], layer['visible'])
        for layer in group_layers(decode_psd('hidden-layer.psd'))
    )
    assert visible['Shape 1']
    assert not visible['Shape 2']
    assert visible['Background']
Beispiel #6
0
def test_layer_visibility():
    visible = dict(
        (layer['name'], layer['visible'])
        for layer in group_layers(decode_psd('hidden-layer.psd'))
    )
    assert visible['Shape 1']
    assert not visible['Shape 2']
    assert visible['Background']
Beispiel #7
0
def test_groups_simple():
    layers = group_layers(decode_psd('group.psd'))
    assert len(layers) == 2

    group = layers[0]
    assert len(group['layers']) == 1
    assert group['name'] == 'Group 1'
    assert group['closed'] == False

    group_element = group['layers'][0]
    assert group_element['name'] == 'Shape 1'
Beispiel #8
0
def test_groups_without_opening():
    layers = group_layers(decode_psd('broken-groups.psd'))
    group1, group2 = layers
    assert group1['name'] == 'bebek'
    assert group2['name'] == 'anne'

    assert len(group1['layers']) == 1
    assert len(group2['layers']) == 1

    assert group1['layers'][0]['name'] == 'el sol'
    assert group2['layers'][0]['name'] == 'kas'
Beispiel #9
0
def test_groups_without_opening():
    layers = group_layers(decode_psd('broken-groups.psd'))
    group1, group2 = layers
    assert group1['name'] == 'bebek'
    assert group2['name'] == 'anne'

    assert len(group1['layers']) == 1
    assert len(group2['layers']) == 1

    assert group1['layers'][0]['name'] == 'el sol'
    assert group2['layers'][0]['name'] == 'kas'
Beispiel #10
0
def test_groups_simple():
    layers = group_layers(decode_psd('group.psd'))
    assert len(layers) == 2

    group = layers[0]
    assert len(group['layers']) == 1
    assert group['name'] == 'Group 1'
    assert group['closed'] == False

    group_element = group['layers'][0]
    assert group_element['name'] == 'Shape 1'
Beispiel #11
0
def main():
    """
    psd-tools.py

    Usage:
        psd-tools.py convert <psd_filename> <out_filename> [options]
        psd-tools.py export_layer <psd_filename> <layer_index> <out_filename> [options]
        psd-tools.py debug <filename> [options]
        psd-tools.py -h | --help
        psd-tools.py --version

    Options:
        -v --verbose                Be more verbose.
        --encoding <encoding>       Text encoding [default: utf8].

    """
    args = docopt.docopt(main.__doc__)

    if args['--verbose']:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)
    encoding = args['--encoding']

    if args['convert']:
        psd = PSDImage.load(args['<psd_filename>'], encoding=encoding)
        im = psd.as_PIL()
        im.save(args['<out_filename>'])

    elif args['export_layer']:
        psd = PSDImage.load(args['<psd_filename>'], encoding=encoding)
        index = int(args['<layer_index>'])
        im = psd.layers[index].as_PIL()
        im.save(args['<out_filename>'])
        print(psd.layers)

        psd.as_PIL()

    elif args['debug']:
        with open(args['<filename>'], "rb") as f:
            decoded = psd_tools.decoder.parse(
                psd_tools.reader.parse(f, encoding)
            )

        print("\nHeader\n------")
        print(decoded.header)
        print("\nDecoded data\n-----------")
        pprint(decoded)
        print("\nLayers\n------")
        pprint(group_layers(decoded))
Beispiel #12
0
def main():
    """
    psd-tools.py

    Usage:
        psd-tools.py convert <psd_filename> <out_filename> [options]
        psd-tools.py export_layer <psd_filename> <layer_index> <out_filename> [options]
        psd-tools.py debug <filename> [options]
        psd-tools.py -h | --help
        psd-tools.py --version

    Options:
        -v --verbose                Be more verbose.
        --encoding <encoding>       Text encoding [default: utf8].

    """
    args = docopt.docopt(main.__doc__, version=__version__)

    if args['--verbose']:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)
    encoding = args['--encoding']

    if args['convert']:
        psd = PSDImage.load(args['<psd_filename>'], encoding=encoding)
        im = psd.as_PIL()
        im.save(args['<out_filename>'])

    elif args['export_layer']:
        psd = PSDImage.load(args['<psd_filename>'], encoding=encoding)
        index = int(args['<layer_index>'])
        im = psd.layers[index].as_PIL()
        im.save(args['<out_filename>'])
        print(psd.layers)

        psd.as_PIL()

    elif args['debug']:
        with open(args['<filename>'], "rb") as f:
            decoded = psd_tools.decoder.parse(
                psd_tools.reader.parse(f, encoding))

        print("\nHeader\n------")
        print(decoded.header)
        print("\nDecoded data\n-----------")
        pprint(decoded)
        print("\nLayers\n------")
        pprint(group_layers(decoded))
Beispiel #13
0
def main():
    """
    psd-tools.py

    Usage:
        psd-tools.py <filename> [--encoding <encoding>] [--verbose]
        psd-tools.py convert <psd_filename> <out_filename> [--verbose]
        psd-tools.py export_layer <psd_filename> <layer_index> <out_filename> [--verbose]
        psd-tools.py -h | --help
        psd-tools.py --version

    Options:
        -v --verbose                Be more verbose.
        --encoding <encoding>       Text encoding [default: utf8].

    """
    args = docopt.docopt(main.__doc__)

    if args['--verbose']:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    if args['convert']:
        psd = PSDImage.load(args['<psd_filename>'])
        im = psd.as_PIL()
        im.save(args['<out_filename>'])

    elif args['export_layer']:
        psd = PSDImage.load(args['<psd_filename>'])
        index = int(args['<layer_index>'])
        im = psd.layers[index].as_PIL()
        im.save(args['<out_filename>'])
        print(psd.layers)

        psd.as_PIL()

    else:
        encoding = args['--encoding']
        with open(args['<filename>'], "rb") as f:
            decoded = psd_tools.decoder.parse(
                psd_tools.reader.parse(f, encoding))

        print(decoded.header)
        pprint.pprint(decoded.image_resource_blocks)
        pprint.pprint(decoded.layer_and_mask_data)
        pprint.pprint(decoded.image_data)
        pprint.pprint(group_layers(decoded))
Beispiel #14
0
def main():
    """
    psd-tools.py

    Usage:
        psd-tools.py <filename> [--encoding <encoding>] [--verbose]
        psd-tools.py convert <psd_filename> <out_filename> [--verbose]
        psd-tools.py export_layer <psd_filename> <layer_index> <out_filename> [--verbose]
        psd-tools.py -h | --help
        psd-tools.py --version

    Options:
        -v --verbose                Be more verbose.
        --encoding <encoding>       Text encoding [default: utf8].

    """
    args = docopt.docopt(main.__doc__)

    if args["--verbose"]:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    if args["convert"]:
        psd = PSDImage.load(args["<psd_filename>"])
        im = psd.as_PIL()
        im.save(args["<out_filename>"])

    elif args["export_layer"]:
        psd = PSDImage.load(args["<psd_filename>"])
        index = int(args["<layer_index>"])
        im = psd.layers[index].as_PIL()
        im.save(args["<out_filename>"])
        print(psd.layers)

        psd.as_PIL()

    else:
        encoding = args["--encoding"]
        with open(args["<filename>"], "rb") as f:
            decoded = psd_tools.decoder.parse(psd_tools.reader.parse(f, encoding))

        print(decoded.header)
        pprint.pprint(decoded.image_resource_blocks)
        pprint.pprint(decoded.layer_and_mask_data)
        pprint.pprint(decoded.image_data)
        pprint.pprint(group_layers(decoded))
Beispiel #15
0
def test_group_visibility():
    layers = group_layers(decode_psd('hidden-groups.psd'))

    group2, group1, bg = layers
    assert group2['name'] == 'Group 2'
    assert group1['name'] == 'Group 1'
    assert bg['name'] == 'Background'

    assert bg['visible'] == True
    assert group2['visible'] == True
    assert group1['visible'] == False

    assert group2['layers'][0]['visible'] == True

    # The flag is 'visible=True', but this layer is hidden
    # because its group is not visible.
    assert group1['layers'][0]['visible'] == True
Beispiel #16
0
def test_group_visibility():
    layers = group_layers(decode_psd('hidden-groups.psd'))

    group2, group1, bg = layers
    assert group2['name'] == 'Group 2'
    assert group1['name'] == 'Group 1'
    assert bg['name'] == 'Background'

    assert bg['visible'] == True
    assert group2['visible'] == True
    assert group1['visible'] == False

    assert group2['layers'][0]['visible'] == True

    # The flag is 'visible=True', but this layer is hidden
    # because its group is not visible.
    assert group1['layers'][0]['visible'] == True
Beispiel #17
0
def test_group_with_empty_layer():
    layers = group_layers(decode_psd('empty-layer.psd'))
    group1, bg = layers
    assert group1['name'] == 'group'
    assert bg['name'] == 'Background'
Beispiel #18
0
def test_groups_32bit():
    layers = group_layers(decode_psd('32bit5x5.psd'))
    assert len(layers) == 3
    assert layers[0]['name'] == 'Background copy 2'
Beispiel #19
0
def test_no_groups():
    layers = group_layers(decode_psd('2layers.psd'))
    assert len(layers) == 2
    assert not any('layers' in layer for layer in layers)
Beispiel #20
0
def test_groups_32bit():
    layers = group_layers(decode_psd('32bit5x5.psd'))
    assert len(layers) == 3
    assert layers[0]['name'] == 'Background copy 2'
Beispiel #21
0
def test_no_groups():
    layers = group_layers(decode_psd('2layers.psd'))
    assert len(layers) == 2
    assert not any('layers' in layer for layer in layers)
Beispiel #22
0
def test_group_with_empty_layer():
    layers = group_layers(decode_psd('empty-layer.psd'))
    group1, bg = layers
    assert group1['name'] == 'group'
    assert bg['name'] == 'Background'