## The Python Imaging Library.# $Id$## XV Thumbnail file handler by Charles E. "Gene" Cash# (gcash@magicnet.net)## see xvcolor.c and xvbrowse.c in the sources to John Bradley's XV,# available from ftp://ftp.cis.upenn.edu/pub/xv/## history:# 98-08-15 cec created (b/w only)# 98-12-09 cec added color palette# 98-12-28 fl added to PIL (with only a few very minor modifications)## To do:# FIXME: make save work (this requires quantization support)#from__future__importannotationsfrom.importImage,ImageFile,ImagePalettefrom._binaryimporto8_MAGIC=b"P7 332"# standard color palette for thumbnails (RGB332)PALETTE=b""forrinrange(8):forginrange(8):forbinrange(4):PALETTE=PALETTE+(o8((r*255)//7)+o8((g*255)//7)+o8((b*255)//3))def_accept(prefix:bytes)->bool:returnprefix.startswith(_MAGIC)### Image plugin for XV thumbnail images.
[docs]classXVThumbImageFile(ImageFile.ImageFile):format="XVThumb"format_description="XV thumbnail image"def_open(self)->None:# check magicassertself.fpisnotNoneifnot_accept(self.fp.read(6)):msg="not an XV thumbnail file"raiseSyntaxError(msg)# Skip to beginning of next lineself.fp.readline()# skip info commentswhileTrue:s=self.fp.readline()ifnots:msg="Unexpected EOF reading XV thumbnail file"raiseSyntaxError(msg)ifs[0]!=35:# ie. when not a comment: '#'break# parse header line (already read)s=s.strip().split()self._mode="P"self._size=int(s[0]),int(s[1])self.palette=ImagePalette.raw("RGB",PALETTE)self.tile=[ImageFile._Tile("raw",(0,0)+self.size,self.fp.tell(),self.mode)]