Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
GdImageFile.py104 linesDownload Raw Back to PIL
1#
2# The Python Imaging Library.
3# $Id$
4#
5# GD file handling
6#
7# History:
8# 1996-04-12 fl   Created
9#
10# Copyright (c) 1997 by Secret Labs AB.
11# Copyright (c) 1996 by Fredrik Lundh.
12#
13# See the README file for information on usage and redistribution.
14#
15
16
17"""
18.. note::
19    This format cannot be automatically recognized, so the
20    class is not registered for use with :py:func:`PIL.Image.open()`.  To open a
21    gd file, use the :py:func:`PIL.GdImageFile.open()` function instead.
22
23.. warning::
24    THE GD FORMAT IS NOT DESIGNED FOR DATA INTERCHANGE.  This
25    implementation is provided for convenience and demonstrational
26    purposes only.
27"""
28
29from __future__ import annotations
30
31from typing import IO
32
33from . import ImageFile, ImagePalette, UnidentifiedImageError
34from ._binary import i16be as i16
35from ._binary import i32be as i32
36from ._typing import StrOrBytesPath
37
38
39class GdImageFile(ImageFile.ImageFile):
40    """
41    Image plugin for the GD uncompressed format.  Note that this format
42    is not supported by the standard :py:func:`PIL.Image.open()` function.  To use
43    this plugin, you have to import the :py:mod:`PIL.GdImageFile` module and
44    use the :py:func:`PIL.GdImageFile.open()` function.
45    """
46
47    format = "GD"
48    format_description = "GD uncompressed images"
49
50    def _open(self) -> None:
51        # Header
52        assert self.fp is not None
53
54        s = self.fp.read(1037)
55
56        if i16(s) not in [65534, 65535]:
57            msg = "Not a valid GD 2.x .gd file"
58            raise SyntaxError(msg)
59
60        self._mode = "P"
61        self._size = i16(s, 2), i16(s, 4)
62
63        true_color = s[6]
64        true_color_offset = 2 if true_color else 0
65
66        # transparency index
67        tindex = i32(s, 7 + true_color_offset)
68        if tindex < 256:
69            self.info["transparency"] = tindex
70
71        self.palette = ImagePalette.raw(
72            "RGBX", s[7 + true_color_offset + 6 : 7 + true_color_offset + 6 + 256 * 4]
73        )
74
75        self.tile = [
76            ImageFile._Tile(
77                "raw",
78                (0, 0) + self.size,
79                7 + true_color_offset + 6 + 256 * 4,
80                "L",
81            )
82        ]
83
84
85def open(fp: StrOrBytesPath | IO[bytes], mode: str = "r") -> GdImageFile:
86    """
87    Load texture from a GD image file.
88
89    :param fp: GD file name, or an opened file handle.
90    :param mode: Optional mode.  In this version, if the mode argument
91        is given, it must be "r".
92    :returns: An image instance.
93    :raises OSError: If the image could not be read.
94    """
95    if mode != "r":
96        msg = "bad mode"
97        raise ValueError(msg)
98
99    try:
100        return GdImageFile(fp)
101    except SyntaxError as e:
102        msg = "cannot identify this image file"
103        raise UnidentifiedImageError(msg) from e
104