Commits

Mikhail Korobov  committed 98ce3f7

(backwards incompatible) consistent function names in pil_support and pymaging_support; more tests

  • Participants
  • Parent commits d7d748b

Comments (0)

Files changed (6)

File src/psd_tools/user_api/pil_support.py

 # -*- coding: utf-8 -*-
-from __future__ import absolute_import
+from __future__ import absolute_import, unicode_literals, division
 
 import warnings
 from psd_tools.utils import be_array_from_bytes
     Image = None
 
 
-def layer_to_PIL(decoded_data, layer_index):
+def extract_layer_image(decoded_data, layer_index):
     """
     Converts a layer from the ``decoded_data`` to a PIL image.
     """
         channels_data, channel_types, size,
         decoded_data.header.depth, get_icc_profile(decoded_data))
 
-def composite_image_to_PIL(decoded_data):
+def extract_composite_image(decoded_data):
     """
     Converts a composite (merged) image from the ``decoded_data``
     to a PIL image.

File src/psd_tools/user_api/psd_image.py

 # -*- coding: utf-8 -*-
-from __future__ import absolute_import, unicode_literals
+from __future__ import absolute_import, unicode_literals, division
 
+import logging
 import collections
 import weakref              # FIXME: there should be weakrefs in this module
 import psd_tools.reader
 import psd_tools.decoder
 from psd_tools.constants import TaggedBlock, SectionDivider
 from psd_tools.user_api.layers import group_layers
-from psd_tools.user_api.pil_support import composite_image_to_PIL, layer_to_PIL
 from psd_tools.user_api import pymaging_support
+from psd_tools.user_api import pil_support
+
+logger = logging.getLogger(__name__)
 
 class BBox(collections.namedtuple('BBox', 'x1, y1, x2, y2')):
     @property
         """
         Returns a PIL image for this PSD file.
         """
-        return composite_image_to_PIL(self.decoded_data)
+        return pil_support.extract_composite_image(self.decoded_data)
 
     def as_pymaging(self):
         """
         return layers[index]
 
     def _layer_as_PIL(self, index):
-        return layer_to_PIL(self.decoded_data, index)
+        return pil_support.extract_layer_image(self.decoded_data, index)
 
     def _layer_as_pymaging(self, index):
         return pymaging_support.extract_layer_image(self.decoded_data, index)

File tests/test_binary.py

+# -*- coding: utf-8 -*-
+from __future__ import absolute_import, unicode_literals
+
+import pytest
+from psd_tools.user_api import pil_support, pymaging_support
+from .utils import decode_psd
+
+def _tobytes(image):
+    try:
+        return image.tobytes() # Pillow at Python 3
+    except AttributeError:
+        try:
+            return image.tostring() # PIL
+        except AttributeError:
+            return image.pixels.data.tostring() # pymaging
+
+SINGLE_LAYER_FILES = [
+    ['1layer.psd'],
+    ['transparentbg-gimp.psd']
+]
+
+BACKENDS = [[pil_support], [pymaging_support]]
+
+@pytest.mark.parametrize(["backend"], BACKENDS)
+@pytest.mark.parametrize(["filename"], SINGLE_LAYER_FILES)
+def test_single_layer(filename, backend):
+    psd = decode_psd(filename)
+
+    composite_image = backend.extract_composite_image(psd)
+    layer_image = backend.extract_layer_image(psd, 0)
+
+    assert len(psd.layer_and_mask_data.layers.layer_records) == 1
+    assert _tobytes(layer_image) == _tobytes(composite_image)
+    assert len(_tobytes(layer_image))

File tests/test_images.py

-# -*- coding: utf-8 -*-
-from __future__ import absolute_import, unicode_literals
-import pytest
-
-from psd_tools import PSDImage
-from psd_tools.user_api.pil_support import composite_image_to_PIL, layer_to_PIL
-from psd_tools.constants import BlendMode
-
-from .utils import decode_psd
-
-def _tobytes(pil_image):
-    try:
-        return pil_image.tobytes()
-    except AttributeError:
-        return pil_image.tostring()
-
-SINGLE_LAYER_FILES = [
-    ['1layer.psd'],
-    ['transparentbg-gimp.psd']
-]
-
-
-@pytest.mark.parametrize(["filename"], SINGLE_LAYER_FILES)
-def test_single_layer(filename):
-    psd = decode_psd(filename)
-    composite_image = composite_image_to_PIL(psd)
-    layer_image = layer_to_PIL(psd, 0)
-
-    assert len(psd.layer_and_mask_data.layers.layer_records) == 1
-    assert _tobytes(layer_image) == _tobytes(composite_image)
-
-def test_api():
-    image = PSDImage(decode_psd('1layer.psd'))
-    assert len(image.layers) == 1
-
-    layer = image.layers[0]
-    assert layer.name == 'Фон'
-    assert layer.bbox == (0, 0, 101, 55)
-    assert layer.visible
-    assert layer.opacity == 255
-    assert layer.blend_mode == BlendMode.NORMAL
-

File tests/test_info.py

+# -*- coding: utf-8 -*-
+from __future__ import absolute_import, unicode_literals
+
+from psd_tools import PSDImage
+from psd_tools.constants import TaggedBlock, SectionDivider, BlendMode
+from .utils import decode_psd
+
+def test_1layer_name():
+    psd = decode_psd('1layer.psd')
+    layers = psd.layer_and_mask_data.layers.layer_records
+    assert len(layers) == 1
+
+    layer = layers[0]
+    assert len(layer.tagged_blocks) == 1
+
+    block = layer.tagged_blocks[0]
+    assert block.key == TaggedBlock.UNICODE_LAYER_NAME
+    assert block.data == 'Фон'
+
+def test_groups():
+    psd = decode_psd('group.psd')
+    layers = psd.layer_and_mask_data.layers.layer_records
+    assert len(layers) == 3+1 # 3 layers + 1 divider
+
+    assert layers[1].tagged_blocks[3].key == TaggedBlock.SECTION_DIVIDER_SETTING
+    assert layers[1].tagged_blocks[3].data.type == SectionDivider.BOUNDING_SECTION_DIVIDER
+
+def test_api():
+    image = PSDImage(decode_psd('1layer.psd'))
+    assert len(image.layers) == 1
+
+    layer = image.layers[0]
+    assert layer.name == 'Фон'
+    assert layer.bbox == (0, 0, 101, 55)
+    assert layer.visible
+    assert layer.opacity == 255
+    assert layer.blend_mode == BlendMode.NORMAL
+

File tests/test_tagged_blocks.py

-# -*- coding: utf-8 -*-
-from __future__ import absolute_import, unicode_literals
-
-from psd_tools.constants import TaggedBlock, SectionDivider
-from .utils import decode_psd
-
-def test_1layer_name():
-    psd = decode_psd('1layer.psd')
-    layers = psd.layer_and_mask_data.layers.layer_records
-    assert len(layers) == 1
-
-    layer = layers[0]
-    assert len(layer.tagged_blocks) == 1
-
-    block = layer.tagged_blocks[0]
-    assert block.key == TaggedBlock.UNICODE_LAYER_NAME
-    assert block.data == 'Фон'
-
-def test_groups():
-    psd = decode_psd('group.psd')
-    layers = psd.layer_and_mask_data.layers.layer_records
-    assert len(layers) == 3+1 # 3 layers + 1 divider
-
-    assert layers[1].tagged_blocks[3].key == TaggedBlock.SECTION_DIVIDER_SETTING
-    assert layers[1].tagged_blocks[3].data.type == SectionDivider.BOUNDING_SECTION_DIVIDER