CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
ImtImagePlugin.py104 linesDownload Raw Back to PIL
1#
2# The Python Imaging Library.
3# $Id$
4#
5# IM Tools support for PIL
6#
7# history:
8# 1996-05-27 fl   Created (read 8-bit images only)
9# 2001-02-17 fl   Use 're' instead of 'regex' (Python 2.1) (0.2)
10#
11# Copyright (c) Secret Labs AB 1997-2001.
12# Copyright (c) Fredrik Lundh 1996-2001.
13#
14# See the README file for information on usage and redistribution.
15#
16from __future__ import annotations
17
18import re
19
20from . import Image, ImageFile
21
22#
23# --------------------------------------------------------------------
24
25field = re.compile(rb"([a-z]*) ([^ \r\n]*)")
26
27
28##
29# Image plugin for IM Tools images.
30
31
32class ImtImageFile(ImageFile.ImageFile):
33    format = "IMT"
34    format_description = "IM Tools"
35
36    def _open(self) -> None:
37        # Quick rejection: if there's not a LF among the first
38        # 100 bytes, this is (probably) not a text header.
39
40        assert self.fp is not None
41
42        buffer = self.fp.read(100)
43        if b"\n" not in buffer:
44            msg = "not an IM file"
45            raise SyntaxError(msg)
46
47        xsize = ysize = 0
48
49        while True:
50            if buffer:
51                s = buffer[:1]
52                buffer = buffer[1:]
53            else:
54                s = self.fp.read(1)
55            if not s:
56                break
57
58            if s == b"\x0c":
59                # image data begins
60                self.tile = [
61                    ImageFile._Tile(
62                        "raw",
63                        (0, 0) + self.size,
64                        self.fp.tell() - len(buffer),
65                        self.mode,
66                    )
67                ]
68
69                break
70
71            else:
72                # read key/value pair
73                if b"\n" not in buffer:
74                    buffer += self.fp.read(100)
75                lines = buffer.split(b"\n")
76                s += lines.pop(0)
77                buffer = b"\n".join(lines)
78                if len(s) == 1 or len(s) > 100:
79                    break
80                if s[0] == ord(b"*"):
81                    continue  # comment
82
83                m = field.match(s)
84                if not m:
85                    break
86                k, v = m.group(1, 2)
87                if k == b"width":
88                    xsize = int(v)
89                    self._size = xsize, ysize
90                elif k == b"height":
91                    ysize = int(v)
92                    self._size = xsize, ysize
93                elif k == b"pixel" and v == b"n8":
94                    self._mode = "L"
95
96
97#
98# --------------------------------------------------------------------
99
100Image.register_open(ImtImageFile.format, ImtImageFile)
101
102#
103# no extension registered (".im" is simply too common)
104 
Aluode/PerceptionLabPortable · CoolFace