# # The Python Imaging Library # $Id$ # # Adobe PSD 2.5/3.0 file handling # # History: # 1995-09-01 fl Created # 1997-01-03 fl Read most PSD images # 1997-01-18 fl Fixed P and CMYK support # 2001-10-21 fl Added seek/tell support (for layers) # # Copyright (c) 1997-2001 by Secret Labs AB. # Copyright (c) 1995-2001 by Fredrik Lundh # # See the README file for information on usage and redistribution. # import io from . import Image, ImageFile, ImagePalette from ._binary import i8 from ._binary import i16be as i16 from ._binary import i32be as i32 MODES = { # (photoshop mode, bits) -> (pil mode, required channels) (0, 1): ("1", 1), (0, 8): ("L", 1), (1, 8): ("L", 1), (2, 8): ("P", 1), (3, 8): ("RGB", 3), (4, 8): ("CMYK", 4), (7, 8): ("L", 1), # FIXME: multilayer (8, 8): ("L", 1), # duotone (9, 8): ("LAB", 3), } # --------------------------------------------------------------------. # read PSD images def _accept(prefix): return prefix[:4] == b"8BPS" ## # Image plugin for Photoshop images. class PsdImageFile(ImageFile.ImageFile): format = "PSD" format_description = "Adobe Photoshop" _close_exclusive_fp_after_loading = False def _open(self): read = self.fp.read # # header s = read(26) if not _accept(s) or i16(s[4:]) != 1: raise SyntaxError("not a PSD file") psd_bits = i16(s[22:]) psd_channels = i16(s[12:]) psd_mode = i16(s[24:]) mode, channels = MODES[(psd_mode, psd_bits)] if channels > psd_channels: raise OSError("not enough channels") self.mode = mode self._size = i32(s[18:]), i32(s[14:]) # # color mode data size = i32(read(4)) if size: data = read(size) if mode == "P" and size == 768: self.palette = ImagePalette.raw("RGB;L", data) # # image resources self.resources = [] size = i32(read(4)) if size: # load resources end = self.fp.tell() + size while self.fp.tell() < end: read(4) # signature id = i16(read(2)) name = read(i8(read(1))) if not (len(name) & 1): read(1) # padding data = read(i32(read(4))) if len(data) & 1: read(1) # padding self.resources.append((id, name, data)) if id == 1039: # ICC profile self.info["icc_profile"] = data # # layer and mask information self.layers = [] size = i32(read(4)) if size: end = self.fp.tell() + size size = i32(read(4)) if size: self.layers = _layerinfo(self.fp) self.fp.seek(end) self.n_frames = len(self.layers) self.is_animated = self.n_frames > 1 # # image descriptor self.tile = _maketile(self.fp, mode, (0, 0) + self.size, channels) # keep the file open self.__fp = self.fp self.frame = 1 self._min_frame = 1 def seek(self, layer): if not self._seek_check(layer): return # seek to given layer (1..max) try: name, mode, bbox, tile = self.layers[layer - 1] self.mode = mode self.tile = tile self.frame = layer self.fp = self.__fp return name, bbox except IndexError as e: raise EOFError("no such layer") from e def tell(self): # return layer number (0=image, 1..max=layers) return self.frame def load_prepare(self): # create image memory if necessary if not self.im or self.im.mode != self.mode or self.im.size != self.size: self.im = Image.core.fill(self.mode, self.size, 0) # create palette (optional) if self.mode == "P": Image.Image.load(self) def _close__fp(self): try: if self.__fp != self.fp: self.__fp.close() except AttributeError: pass finally: self.__fp = None def _layerinfo(file): # read layerinfo block layers = [] read = file.read for i in range(abs(i16(read(2)))): # bounding box y0 = i32(read(4)) x0 = i32(read(4)) y1 = i32(read(4)) x1 = i32(read(4)) # image info info = [] mode = [] types = list(range(i16(read(2)))) if len(types) > 4: continue for i in types: type = i16(read(2)) if type == 65535: m = "A" else: m = "RGBA"[type] mode.append(m) size = i32(read(4)) info.append((m, size)) # figure out the image mode mode.sort() if mode == ["R"]: mode = "L" elif mode == ["B", "G", "R"]: mode = "RGB" elif mode == ["A", "B", "G", "R"]: mode = "RGBA" else: mode = None # unknown # skip over blend flags and extra information read(12) # filler name = "" size = i32(read(4)) # length of the extra data field combined = 0 if size: data_end = file.tell() + size length = i32(read(4)) if length: file.seek(length - 16, io.SEEK_CUR) combined += length + 4 length = i32(read(4)) if length: file.seek(length, io.SEEK_CUR) combined += length + 4 length = i8(read(1)) if length: # Don't know the proper encoding, # Latin-1 should be a good guess name = read(length).decode("latin-1", "replace") combined += length + 1 file.seek(data_end) layers.append((name, mode, (x0, y0, x1, y1))) # get tiles i = 0 for name, mode, bbox in layers: tile = [] for m in mode: t = _maketile(file, m, bbox, 1) if t: tile.extend(t) layers[i] = name, mode, bbox, tile i += 1 return layers def _maketile(file, mode, bbox, channels): tile = None read = file.read compression = i16(read(2)) xsize = bbox[2] - bbox[0] ysize = bbox[3] - bbox[1] offset = file.tell() if compression == 0: # # raw compression tile = [] for channel in range(channels): layer = mode[channel] if mode == "CMYK": layer += ";I" tile.append(("raw", bbox, offset, layer)) offset = offset + xsize * ysize elif compression == 1: # # packbits compression i = 0 tile = [] bytecount = read(channels * ysize * 2) offset = file.tell() for channel in range(channels): layer = mode[channel] if mode == "CMYK": layer += ";I" tile.append(("packbits", bbox, offset, layer)) for y in range(ysize): offset = offset + i16(bytecount[i : i + 2]) i += 2 file.seek(offset) if offset & 1: read(1) # padding return tile # -------------------------------------------------------------------- # registry Image.register_open(PsdImageFile.format, PsdImageFile, _accept) Image.register_extension(PsdImageFile.format, ".psd") Image.register_mime(PsdImageFile.format, "image/vnd.adobe.photoshop")
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 2755 |
|
|
BdfFontFile.py | File | 2.75 KB | 0644 |
|
BlpImagePlugin.py | File | 14 KB | 0644 |
|
BmpImagePlugin.py | File | 13.92 KB | 0644 |
|
BufrStubImagePlugin.py | File | 1.48 KB | 0644 |
|
ContainerIO.py | File | 2.82 KB | 0644 |
|
CurImagePlugin.py | File | 1.68 KB | 0644 |
|
DcxImagePlugin.py | File | 2.09 KB | 0644 |
|
DdsImagePlugin.py | File | 5.34 KB | 0644 |
|
EpsImagePlugin.py | File | 11.82 KB | 0644 |
|
ExifTags.py | File | 8.8 KB | 0644 |
|
FitsStubImagePlugin.py | File | 1.59 KB | 0644 |
|
FliImagePlugin.py | File | 4.23 KB | 0644 |
|
FontFile.py | File | 2.7 KB | 0644 |
|
FpxImagePlugin.py | File | 6.53 KB | 0644 |
|
FtexImagePlugin.py | File | 3.23 KB | 0644 |
|
GbrImagePlugin.py | File | 2.73 KB | 0644 |
|
GdImageFile.py | File | 2.47 KB | 0644 |
|
GifImagePlugin.py | File | 28.25 KB | 0644 |
|
GimpGradientFile.py | File | 3.27 KB | 0644 |
|
GimpPaletteFile.py | File | 1.24 KB | 0644 |
|
GribStubImagePlugin.py | File | 1.51 KB | 0644 |
|
Hdf5StubImagePlugin.py | File | 1.48 KB | 0644 |
|
IcnsImagePlugin.py | File | 11.44 KB | 0644 |
|
IcoImagePlugin.py | File | 9.94 KB | 0644 |
|
ImImagePlugin.py | File | 10.53 KB | 0644 |
|
Image.py | File | 113.4 KB | 0644 |
|
ImageChops.py | File | 7.13 KB | 0644 |
|
ImageCms.py | File | 36.22 KB | 0644 |
|
ImageColor.py | File | 8.44 KB | 0644 |
|
ImageDraw.py | File | 29.94 KB | 0644 |
|
ImageDraw2.py | File | 4.9 KB | 0644 |
|
ImageEnhance.py | File | 3.12 KB | 0644 |
|
ImageFile.py | File | 20.74 KB | 0644 |
|
ImageFilter.py | File | 15.46 KB | 0644 |
|
ImageFont.py | File | 43.49 KB | 0644 |
|
ImageGrab.py | File | 3.54 KB | 0644 |
|
ImageMath.py | File | 6.88 KB | 0644 |
|
ImageMode.py | File | 1.6 KB | 0644 |
|
ImageMorph.py | File | 7.67 KB | 0644 |
|
ImageOps.py | File | 18.03 KB | 0644 |
|
ImagePalette.py | File | 6.2 KB | 0644 |
|
ImagePath.py | File | 336 B | 0644 |
|
ImageQt.py | File | 5.67 KB | 0644 |
|
ImageSequence.py | File | 1.81 KB | 0644 |
|
ImageShow.py | File | 6.15 KB | 0644 |
|
ImageStat.py | File | 3.81 KB | 0644 |
|
ImageTk.py | File | 9.11 KB | 0644 |
|
ImageTransform.py | File | 2.78 KB | 0644 |
|
ImageWin.py | File | 7.02 KB | 0644 |
|
ImtImagePlugin.py | File | 2.15 KB | 0644 |
|
IptcImagePlugin.py | File | 5.6 KB | 0644 |
|
Jpeg2KImagePlugin.py | File | 8.52 KB | 0644 |
|
JpegImagePlugin.py | File | 27.16 KB | 0644 |
|
JpegPresets.py | File | 12.41 KB | 0644 |
|
McIdasImagePlugin.py | File | 1.71 KB | 0644 |
|
MicImagePlugin.py | File | 2.54 KB | 0644 |
|
MpegImagePlugin.py | File | 1.76 KB | 0644 |
|
MpoImagePlugin.py | File | 4.14 KB | 0644 |
|
MspImagePlugin.py | File | 5.43 KB | 0644 |
|
PSDraw.py | File | 6.51 KB | 0644 |
|
PaletteFile.py | File | 1.08 KB | 0644 |
|
PalmImagePlugin.py | File | 8.89 KB | 0644 |
|
PcdImagePlugin.py | File | 1.47 KB | 0644 |
|
PcfFontFile.py | File | 6.2 KB | 0644 |
|
PcxImagePlugin.py | File | 5.41 KB | 0644 |
|
PdfImagePlugin.py | File | 7.49 KB | 0644 |
|
PdfParser.py | File | 33.58 KB | 0644 |
|
PixarImagePlugin.py | File | 1.61 KB | 0644 |
|
PngImagePlugin.py | File | 42.79 KB | 0644 |
|
PpmImagePlugin.py | File | 4.34 KB | 0644 |
|
PsdImagePlugin.py | File | 7.56 KB | 0644 |
|
PyAccess.py | File | 9.37 KB | 0644 |
|
SgiImagePlugin.py | File | 5.96 KB | 0644 |
|
SpiderImagePlugin.py | File | 9.31 KB | 0644 |
|
SunImagePlugin.py | File | 4.2 KB | 0644 |
|
TarIO.py | File | 1.41 KB | 0644 |
|
TgaImagePlugin.py | File | 6.18 KB | 0644 |
|
TiffImagePlugin.py | File | 66.86 KB | 0644 |
|
TiffTags.py | File | 14.22 KB | 0644 |
|
WalImageFile.py | File | 5.4 KB | 0644 |
|
WebPImagePlugin.py | File | 10.54 KB | 0644 |
|
WmfImagePlugin.py | File | 4.56 KB | 0644 |
|
XVThumbImagePlugin.py | File | 1.9 KB | 0644 |
|
XbmImagePlugin.py | File | 2.37 KB | 0644 |
|
XpmImagePlugin.py | File | 3 KB | 0644 |
|
__init__.py | File | 3.18 KB | 0644 |
|
__main__.py | File | 41 B | 0644 |
|
_binary.py | File | 1.75 KB | 0644 |
|
_imaging.cpython-36m-x86_64-linux-gnu.so | File | 650.13 KB | 0755 |
|
_imagingcms.cpython-36m-x86_64-linux-gnu.so | File | 38.05 KB | 0755 |
|
_imagingft.cpython-36m-x86_64-linux-gnu.so | File | 41.98 KB | 0755 |
|
_imagingmath.cpython-36m-x86_64-linux-gnu.so | File | 24.43 KB | 0755 |
|
_imagingmorph.cpython-36m-x86_64-linux-gnu.so | File | 8.12 KB | 0755 |
|
_imagingtk.cpython-36m-x86_64-linux-gnu.so | File | 9.37 KB | 0755 |
|
_tkinter_finder.py | File | 224 B | 0644 |
|
_util.py | File | 359 B | 0644 |
|
_version.py | File | 50 B | 0644 |
|
_webp.cpython-36m-x86_64-linux-gnu.so | File | 40.82 KB | 0755 |
|
features.py | File | 8.8 KB | 0644 |
|