Пример #1
0
    def open(cls, fp):
        """
        Open a PSD document.

        :param fp: filename or file-like object.
        :return: A :py:class:`~psd_tools.api.psd_image.PSDImage` object.
        """
        if hasattr(fp, 'read'):
            self = cls(PSD.read(fp))
        else:
            with open(fp, 'rb') as f:
                self = cls(PSD.read(f))
        return self
Пример #2
0
    def open(cls, fp, **kwargs):
        """
        Open a PSD document.

        :param fp: filename or file-like object.
        :param encoding: charset encoding of the pascal string within the file,
            default 'macroman'. Some psd files need explicit encoding option.
        :return: A :py:class:`~psd_tools.api.psd_image.PSDImage` object.
        """
        if hasattr(fp, 'read'):
            self = cls(PSD.read(fp, **kwargs))
        else:
            with open(fp, 'rb') as f:
                self = cls(PSD.read(f, **kwargs))
        return self
Пример #3
0
    def new(cls, mode, size, color=0, depth=8, **kwargs):
        """
        Create a new PSD document.

        :param mode: The color mode to use for the new image.
        :param size: A tuple containing (width, height) in pixels.
        :param color: What color to use for the image. Default is black.
        :return: A :py:class:`~psd_tools.api.psd_image.PSDImage` object.
        """
        header = cls._make_header(mode, size, depth)
        image_data = ImageData.new(header, color=color, **kwargs)
        # TODO: Add default metadata.
        return cls(PSD(
            header=header,
            image_data=image_data,
            image_resources=ImageResources.new(),
        ))
Пример #4
0
def test_psd_read_write(filename):
    basename = os.path.basename(filename)
    with open(filename, 'rb') as f:
        expected = f.read()

    with io.BytesIO(expected) as f:
        psd = PSD.read(f)

    padding = BAD_PADDINGS.get(basename, 4)
    with io.BytesIO() as f:
        psd.write(f, padding=padding)
        f.flush()
        output = f.getvalue()

    if basename in BAD_UNICODE_PADDINGS:
        pytest.xfail('Broken file')
    assert len(output) == len(expected)
    assert output == expected
Пример #5
0
    def frompil(cls, image, compression=Compression.PACK_BITS):
        """
        Create a new PSD document from PIL Image.

        :param image: PIL Image object.
        :param compression: ImageData compression option. See
            :py:class:`~psd_tools.constants.Compression`.
        :return: A :py:class:`~psd_tools.api.psd_image.PSDImage` object.
        """
        header = cls._make_header(image.mode, image.size)
        # TODO: Add default metadata.
        # TODO: Perhaps make this smart object.
        image_data = ImageData(compression=compression)
        image_data.set_data([channel.tobytes() for channel in image.split()], header)
        return cls(PSD(
            header=header,
            image_data=image_data,
            image_resources=ImageResources.new(),
        ))
Пример #6
0
def test_psd_from_error():
    with pytest.raises(AssertionError):
        PSD.frombytes(b'\x00\x00\x00\x00')
Пример #7
0
def test_psd_write_read(filename):
    with open(filename, 'rb') as f:
        psd = PSD.read(f)
    check_write_read(psd)
    check_write_read(psd, encoding='utf_8')
Пример #8
0
def test_psd__iter_layers(filename, length):
    with open(os.path.join(TEST_ROOT, 'psd_files', filename), 'rb') as f:
        psd = PSD.read(f)
    assert len(list(psd._iter_layers())) == length