Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
Pillow (PIL Fork) 12.0.0 documentation
Light LogoDark Logo
Pillow (PIL Fork) 12.0.0 documentation
Back to top

Source code for PIL.PcxImagePlugin

## The Python Imaging Library.# $Id$## PCX file handling## This format was originally used by ZSoft's popular PaintBrush# program for the IBM PC.  It is also supported by many MS-DOS and# Windows applications, including the Windows PaintBrush program in# Windows 3.## history:# 1995-09-01 fl   Created# 1996-05-20 fl   Fixed RGB support# 1997-01-03 fl   Fixed 2-bit and 4-bit support# 1999-02-03 fl   Fixed 8-bit support (broken in 1.0b1)# 1999-02-07 fl   Added write support# 2002-06-09 fl   Made 2-bit and 4-bit support a bit more robust# 2002-07-30 fl   Seek from to current position, not beginning of file# 2003-06-03 fl   Extract DPI settings (info["dpi"])## Copyright (c) 1997-2003 by Secret Labs AB.# Copyright (c) 1995-2003 by Fredrik Lundh.## See the README file for information on usage and redistribution.#from__future__importannotationsimportioimportloggingfromtypingimportIOfrom.importImage,ImageFile,ImagePalettefrom._binaryimporti16leasi16from._binaryimporto8from._binaryimporto16leaso16logger=logging.getLogger(__name__)def_accept(prefix:bytes)->bool:returnlen(prefix)>=2andprefix[0]==10andprefix[1]in[0,2,3,5]### Image plugin for Paintbrush images.
[docs]classPcxImageFile(ImageFile.ImageFile):format="PCX"format_description="Paintbrush"def_open(self)->None:# headerassertself.fpisnotNones=self.fp.read(68)ifnot_accept(s):msg="not a PCX file"raiseSyntaxError(msg)# imagebbox=i16(s,4),i16(s,6),i16(s,8)+1,i16(s,10)+1ifbbox[2]<=bbox[0]orbbox[3]<=bbox[1]:msg="bad PCX image size"raiseSyntaxError(msg)logger.debug("BBox:%s%s%s%s",*bbox)offset=self.fp.tell()+60# formatversion=s[1]bits=s[3]planes=s[65]provided_stride=i16(s,66)logger.debug("PCX version%s, bits%s, planes%s, stride%s",version,bits,planes,provided_stride,)self.info["dpi"]=i16(s,12),i16(s,14)ifbits==1andplanes==1:mode=rawmode="1"elifbits==1andplanesin(2,4):mode="P"rawmode=f"P;{planes}L"self.palette=ImagePalette.raw("RGB",s[16:64])elifversion==5andbits==8andplanes==1:mode=rawmode="L"# FIXME: hey, this doesn't work with the incremental loader !!!self.fp.seek(-769,io.SEEK_END)s=self.fp.read(769)iflen(s)==769ands[0]==12:# check if the palette is linear grayscaleforiinrange(256):ifs[i*3+1:i*3+4]!=o8(i)*3:mode=rawmode="P"breakifmode=="P":self.palette=ImagePalette.raw("RGB",s[1:])elifversion==5andbits==8andplanes==3:mode="RGB"rawmode="RGB;L"else:msg="unknown PCX mode"raiseOSError(msg)self._mode=modeself._size=bbox[2]-bbox[0],bbox[3]-bbox[1]# Don't trust the passed in stride.# Calculate the approximate position for ourselves.# CVE-2020-35653stride=(self._size[0]*bits+7)//8# While the specification states that this must be even,# not all images follow thisifprovided_stride!=stride:stride+=stride%2bbox=(0,0)+self.sizelogger.debug("size:%sx%s",*self.size)self.tile=[ImageFile._Tile("pcx",bbox,offset,(rawmode,planes*stride))]
# --------------------------------------------------------------------# save PCX filesSAVE={# mode: (version, bits, planes, raw mode)"1":(2,1,1,"1"),"L":(5,8,1,"L"),"P":(5,8,1,"P"),"RGB":(5,8,3,"RGB;L"),}def_save(im:Image.Image,fp:IO[bytes],filename:str|bytes)->None:try:version,bits,planes,rawmode=SAVE[im.mode]exceptKeyErrorase:msg=f"Cannot save{im.mode} images as PCX"raiseValueError(msg)frome# bytes per planestride=(im.size[0]*bits+7)//8# stride should be evenstride+=stride%2# Stride needs to be kept in sync with the PcxEncode.c version.# Ideally it should be passed in in the state, but the bytes value# gets overwritten.logger.debug("PcxImagePlugin._save: xwidth:%d, bits:%d, stride:%d",im.size[0],bits,stride,)# under windows, we could determine the current screen size with# "Image.core.display_mode()[1]", but I think that's overkill...screen=im.sizedpi=100,100# PCX headerfp.write(o8(10)+o8(version)+o8(1)+o8(bits)+o16(0)+o16(0)+o16(im.size[0]-1)+o16(im.size[1]-1)+o16(dpi[0])+o16(dpi[1])+b"\0"*24+b"\xff"*24+b"\0"+o8(planes)+o16(stride)+o16(1)+o16(screen[0])+o16(screen[1])+b"\0"*54)assertfp.tell()==128ImageFile._save(im,fp,[ImageFile._Tile("pcx",(0,0)+im.size,0,(rawmode,bits*planes))])ifim.mode=="P":# colour palettefp.write(o8(12))palette=im.im.getpalette("RGB","RGB")palette+=b"\x00"*(768-len(palette))fp.write(palette)# 768 byteselifim.mode=="L":# grayscale palettefp.write(o8(12))foriinrange(256):fp.write(o8(i)*3)# --------------------------------------------------------------------# registryImage.register_open(PcxImageFile.format,PcxImageFile,_accept)Image.register_save(PcxImageFile.format,_save)Image.register_extension(PcxImageFile.format,".pcx")Image.register_mime(PcxImageFile.format,"image/x-pcx")
Copyright © 1995-2011 Fredrik Lundh and contributors, 2010 Jeffrey A. Clark and contributors.
Made withSphinx and@pradyunsg'sFuro

[8]
ページ先頭

©2009-2025 Movatter.jp