Commits

Mikhail Korobov committed 47dc178

_index is added to layer info; minor renames

Comments (0)

Files changed (2)

 import psd_tools.reader
 import psd_tools.decoder
 from psd_tools import user_api
-from psd_tools.user_api.layers import make_layers, image_to_PIL
+from psd_tools.user_api.layers import group_layers, image_to_PIL
 
 logger = logging.getLogger('psd_tools')
 logger.addHandler(logging.StreamHandler())
 
         print(decoded.header)
         pprint.pprint(decoded.image_resource_blocks)
-        pprint.pprint(make_layers(decoded))
+        pprint.pprint(group_layers(decoded))

psd_tools/user_api/layers.py

 from psd_tools.constants import (Compression, ChannelID, ColorMode,
                                  TaggedBlock, SectionDivider)
 
-def make_layers(raw_data):
-    layer_records = raw_data.layer_and_mask_data.layers.layer_records
+
+def group_layers(decoded_data):
+    """
+    Returns a nested dict with PSD layer group information.
+    """
+    layer_records = decoded_data.layer_and_mask_data.layers.layer_records
 
     root = dict(layers = [])
     group_stack = [root]
         divider = blocks.get(TaggedBlock.SECTION_DIVIDER_SETTING, None)
         visible = layer.flags.visible
         opacity = layer.opacity
+        blend_mode = layer.blend_mode
 
         if divider is not None:
             # group information
                 # group begins
                 group = dict(
                     id = layer_id,
+                    _index = index,
                     name = name,
                     layers = [],
                     closed = divider.type == SectionDivider.CLOSED_FOLDER,
-                    blend_mode = layer.blend_mode,
+
+                    blend_mode = blend_mode,
                     visible = visible,
                     opacity = opacity,
                 )
                 warnings.warn("invalid state")
         else:
             # layer with image
-            image = layer_to_PIL(raw_data, index)
+            image = layer_to_PIL(decoded_data, index)
+
             current_group['layers'].append(dict(
                 id = layer_id,
+                _index = index,
                 name = name,
                 image = image,
 
                 bottom = layer.bottom,
                 right = layer.right,
 
-                blend_mode = layer.blend_mode,
+                blend_mode = blend_mode,
                 visible = visible,
                 opacity = opacity,
             ))
 
     return root['layers']
 
-
 def _get_mode(band_keys):
     for mode in ['RGBA', 'RGB']:
         if set(band_keys) == set(list(mode)):
     return Image.merge(mode, [bands[band] for band in mode])
 
 
-def layer_to_PIL(parsed_data, layer_num):
-    layers = parsed_data.layer_and_mask_data.layers
+def layer_to_PIL(decoded_data, layer_num):
+    layers = decoded_data.layer_and_mask_data.layers
     layer = layers.layer_records[layer_num]
 
     channels_data = layers.channel_image_data[layer_num]
 
     return _channels_data_to_PIL(channels_data, channel_types, size)
 
-def image_to_PIL(parsed_data):
-    header = parsed_data.header
+def image_to_PIL(decoded_data):
+    header = decoded_data.header
     size = header.width, header.height
 
     if header.color_mode == ColorMode.RGB:
         return
 
     return _channels_data_to_PIL(
-        parsed_data.image_data,
+        decoded_data.image_data,
         channel_types,
         size,
     )