Пример #1
0
 def __repr__(self):
     return (
         "PsdHeader(version=%s, number_of_channels=%s, height=%s, "
         "width=%s, depth=%s, color_mode=%s)" % (
             self.version, self.number_of_channels, self.height,
             self.width, self.depth, ColorMode.name_of(self.color_mode)
         )
     )
Пример #2
0
 def __repr__(self):
     return "PsdHeader(number_of_channels=%s, height=%s, width=%s, depth=%s, color_mode=%s)" % (
         self.number_of_channels,
         self.height,
         self.width,
         self.depth,
         ColorMode.name_of(self.color_mode),
     )
Пример #3
0
 def _make_header(cls, mode, size, depth=8):
     assert depth in (8, 16, 32), 'Invalid depth: %d' % (depth)
     color_mode = pil_io.get_color_mode(mode)
     alpha = int(mode.upper().endswith('A'))
     channels = ColorMode.channels(color_mode, alpha)
     return FileHeader(width=size[0],
                       height=size[1],
                       depth=depth,
                       channels=channels,
                       color_mode=color_mode)
Пример #4
0
def _check_channels(channels, color_mode):
    expected_channels = ColorMode.channels(color_mode)
    if len(channels) > expected_channels:
        logger.warning('Channels mismatch: expected %g != given %g' %
                       (expected_channels, len(channels)))
        channels = channels[:expected_channels]
    elif len(channels) < expected_channels:
        raise ValueError('Channels mismatch: expected %g != given %g' %
                         (expected_channels, len(channels)))
    return channels
Пример #5
0
def _check_channels(channels, color_mode):
    expected_channels = ColorMode.channels(color_mode)
    if len(channels) > expected_channels:
        # Seems possible when FilterMask is attached.
        logger.debug('Channels mismatch: expected %g != given %g' %
                     (expected_channels, len(channels)))
        channels = channels[:expected_channels]
    elif len(channels) < expected_channels:
        raise ValueError('Channels mismatch: expected %g != given %g' %
                         (expected_channels, len(channels)))
    return channels
Пример #6
0
    def read(cls, fp, **kwargs):
        version = read_fmt('I', fp)[0]
        assert version == 1, 'Invalid version %d' % (version)
        image_mode = ColorMode(read_fmt('I', fp)[0])
        point = read_fmt('2h', fp)
        name = read_unicode_string(fp)
        pattern_id = read_pascal_string(fp, encoding='ascii', padding=1)
        color_table = None
        if image_mode == ColorMode.INDEXED:
            color_table = [read_fmt("3B", fp) for i in range(256)]
            read_fmt('4x', fp)

        data = VirtualMemoryArrayList.read(fp)
        return cls(version, image_mode, point, name, pattern_id, color_table,
                   data)
Пример #7
0
 def _make_header(cls, mode, size, depth=8):
     from .pil_io import get_color_mode
     assert depth in (8, 16, 32), 'Invalid depth: %d' % (depth)
     assert size[0] <= 300000, 'Width too large > 300,000'
     assert size[1] <= 300000, 'Height too large > 300,000'
     version = 1
     if size[0] > 30000 or size[1] > 30000:
         logger.debug('Width or height larger than 30,000 pixels')
         version = 2
     color_mode = get_color_mode(mode)
     alpha = int(mode.upper().endswith('A'))
     channels = ColorMode.channels(color_mode, alpha)
     return FileHeader(version=version,
                       width=size[0],
                       height=size[1],
                       depth=depth,
                       channels=channels,
                       color_mode=color_mode)
Пример #8
0
def read(fp):
    """
    Reads PSD file header.
    """
    logger.debug("reading header..")
    signature = fp.read(4)
    if signature != b"8BPS":
        raise Error("This is not a PSD file")

    version = read_fmt("H", fp)[0]
    if version != 1:
        raise Error("Unsupported PSD version (%s)" % version)

    header = PsdHeader(*read_fmt("6x HIIHH", fp))

    if not ColorMode.is_known(header.color_mode):
        warnings.warn("Unknown color mode: %s" % header.color_mode)

    logger.debug(header)
    return header
Пример #9
0
def read(fp):
    """
    Reads PSD file header.
    """
    logger.debug("reading header..")
    signature = fp.read(4)
    if signature != b'8BPS':
        raise Error("This is not a PSD or PSB file")

    version = read_fmt("H", fp)[0]
    if not version in (1, 2):
        raise Error("Unsupported PSD version (%s)" % version)

    header = PsdHeader(version, *read_fmt("6x HIIHH", fp))

    if not ColorMode.is_known(header.color_mode):
        warnings.warn("Unknown color mode: %s" % header.color_mode)

    logger.debug(header)
    return header
Пример #10
0
def _validate_header(header):
    """
    Validates header and returns (depth, mode) tuple.
    """
    if LoadedImage is None or packbits is None:
        raise Exception("This module requires `pymaging` and `packbits` packages.")

    if header.color_mode != ColorMode.RGB:
        raise NotImplementedError(
            "This color mode (%s) is not supported yet" % ColorMode.name_of(header.color_mode)
        )

    mode = _get_mode(header.number_of_channels)
    if mode is None:
        raise NotImplementedError("This number of channels (%d) is unsupported for this color mode (%s)" % (
                         header.number_of_channels, header.color_mode))

    if header.depth != 8:
        raise NotImplementedError("Only 8bit images are currently supported with pymaging.")

    return 8, mode
Пример #11
0
def _validate_header(header):
    """
    Validates header and returns (depth, mode) tuple.
    """
    if LoadedImage is None or packbits is None:
        raise Exception(
            "This module requires `pymaging` and `packbits` packages.")

    if header.color_mode != ColorMode.RGB:
        raise NotImplementedError("This color mode (%s) is not supported yet" %
                                  ColorMode.name_of(header.color_mode))

    mode = _get_mode(header.number_of_channels)
    if mode is None:
        raise NotImplementedError(
            "This number of channels (%d) is unsupported for this color mode"
            " (%s)" % (header.number_of_channels, header.color_mode))

    if header.depth != 8:
        raise NotImplementedError(
            "Only 8bit images are currently supported with pymaging.")

    return 8, mode