CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
XVThumbImagePlugin.py84 linesDownload Raw Back to PIL
1#
2# The Python Imaging Library.
3# $Id$
4#
5# XV Thumbnail file handler by Charles E. "Gene" Cash
6# (gcash@magicnet.net)
7#
8# see xvcolor.c and xvbrowse.c in the sources to John Bradley's XV,
9# available from ftp://ftp.cis.upenn.edu/pub/xv/
10#
11# history:
12# 98-08-15 cec  created (b/w only)
13# 98-12-09 cec  added color palette
14# 98-12-28 fl   added to PIL (with only a few very minor modifications)
15#
16# To do:
17# FIXME: make save work (this requires quantization support)
18#
19from __future__ import annotations
20
21from . import Image, ImageFile, ImagePalette
22from ._binary import o8
23
24_MAGIC = b"P7 332"
25
26# standard color palette for thumbnails (RGB332)
27PALETTE = b""
28for r in range(8):
29    for g in range(8):
30        for b in range(4):
31            PALETTE = PALETTE + (
32                o8((r * 255) // 7) + o8((g * 255) // 7) + o8((b * 255) // 3)
33            )
34
35
36def _accept(prefix: bytes) -> bool:
37    return prefix.startswith(_MAGIC)
38
39
40##
41# Image plugin for XV thumbnail images.
42
43
44class XVThumbImageFile(ImageFile.ImageFile):
45    format = "XVThumb"
46    format_description = "XV thumbnail image"
47
48    def _open(self) -> None:
49        # check magic
50        assert self.fp is not None
51
52        if not _accept(self.fp.read(6)):
53            msg = "not an XV thumbnail file"
54            raise SyntaxError(msg)
55
56        # Skip to beginning of next line
57        self.fp.readline()
58
59        # skip info comments
60        while True:
61            s = self.fp.readline()
62            if not s:
63                msg = "Unexpected EOF reading XV thumbnail file"
64                raise SyntaxError(msg)
65            if s[0] != 35:  # ie. when not a comment: '#'
66                break
67
68        # parse header line (already read)
69        w, h = s.strip().split(maxsplit=2)[:2]
70
71        self._mode = "P"
72        self._size = int(w), int(h)
73
74        self.palette = ImagePalette.raw("RGB", PALETTE)
75
76        self.tile = [
77            ImageFile._Tile("raw", (0, 0) + self.size, self.fp.tell(), self.mode)
78        ]
79
80
81# --------------------------------------------------------------------
82
83Image.register_open(XVThumbImageFile.format, XVThumbImageFile, _accept)
84 
Aluode/PerceptionLabPortable · CoolFace