2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
2019-01-13 05:05:46 +03:00
|
|
|
import io
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2017-01-17 16:22:18 +03:00
|
|
|
from . import Image, ImageFile, ImagePalette
|
2020-08-07 13:28:33 +03:00
|
|
|
from ._binary import i8
|
|
|
|
from ._binary import i16be as i16
|
|
|
|
from ._binary import i32be as i32
|
2021-07-15 12:38:26 +03:00
|
|
|
from ._binary import si16be as si16
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
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),
|
2014-08-26 17:47:10 +04:00
|
|
|
(7, 8): ("L", 1), # FIXME: multilayer
|
|
|
|
(8, 8): ("L", 1), # duotone
|
2019-03-21 16:28:20 +03:00
|
|
|
(9, 8): ("LAB", 3),
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
# --------------------------------------------------------------------.
|
|
|
|
# read PSD images
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def _accept(prefix):
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
return prefix[:4] == b"8BPS"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
##
|
|
|
|
# Image plugin for Photoshop images.
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
class PsdImageFile(ImageFile.ImageFile):
|
|
|
|
format = "PSD"
|
|
|
|
format_description = "Adobe Photoshop"
|
2019-03-31 14:24:10 +03:00
|
|
|
_close_exclusive_fp_after_loading = False
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
read = self.fp.read
|
|
|
|
|
|
|
|
#
|
|
|
|
# header
|
|
|
|
|
|
|
|
s = read(26)
|
2020-05-08 21:11:58 +03:00
|
|
|
if not _accept(s) or i16(s, 4) != 1:
|
2022-12-22 00:51:35 +03:00
|
|
|
msg = "not a PSD file"
|
|
|
|
raise SyntaxError(msg)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-08 21:11:58 +03:00
|
|
|
psd_bits = i16(s, 22)
|
|
|
|
psd_channels = i16(s, 12)
|
|
|
|
psd_mode = i16(s, 24)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
mode, channels = MODES[(psd_mode, psd_bits)]
|
|
|
|
|
|
|
|
if channels > psd_channels:
|
2022-12-22 00:51:35 +03:00
|
|
|
msg = "not enough channels"
|
|
|
|
raise OSError(msg)
|
2022-08-05 16:29:58 +03:00
|
|
|
if mode == "RGB" and psd_channels == 4:
|
|
|
|
mode = "RGBA"
|
|
|
|
channels = 4
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
self.mode = mode
|
2020-05-08 21:11:58 +03:00
|
|
|
self._size = i32(s, 18), i32(s, 14)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# 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:
|
2018-10-18 21:49:14 +03:00
|
|
|
read(4) # signature
|
2010-07-31 06:52:47 +04:00
|
|
|
id = i16(read(2))
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
name = read(i8(read(1)))
|
2010-07-31 06:52:47 +04:00
|
|
|
if not (len(name) & 1):
|
2014-08-26 17:47:10 +04:00
|
|
|
read(1) # padding
|
2010-07-31 06:52:47 +04:00
|
|
|
data = read(i32(read(4)))
|
2019-03-21 16:28:20 +03:00
|
|
|
if len(data) & 1:
|
2014-08-26 17:47:10 +04:00
|
|
|
read(1) # padding
|
2010-07-31 06:52:47 +04:00
|
|
|
self.resources.append((id, name, data))
|
2014-08-26 17:47:10 +04:00
|
|
|
if id == 1039: # ICC profile
|
2010-07-31 06:52:47 +04:00
|
|
|
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:
|
2021-03-07 21:04:25 +03:00
|
|
|
_layer_data = io.BytesIO(ImageFile._safe_read(self.fp, size))
|
|
|
|
self.layers = _layerinfo(_layer_data, size)
|
2010-07-31 06:52:47 +04:00
|
|
|
self.fp.seek(end)
|
2020-04-13 07:37:49 +03:00
|
|
|
self.n_frames = len(self.layers)
|
|
|
|
self.is_animated = self.n_frames > 1
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# image descriptor
|
|
|
|
|
|
|
|
self.tile = _maketile(self.fp, mode, (0, 0) + self.size, channels)
|
|
|
|
|
|
|
|
# keep the file open
|
2022-04-13 02:54:17 +03:00
|
|
|
self._fp = self.fp
|
2017-09-06 06:23:50 +03:00
|
|
|
self.frame = 1
|
|
|
|
self._min_frame = 1
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def seek(self, layer):
|
2017-09-30 06:32:43 +03:00
|
|
|
if not self._seek_check(layer):
|
2010-07-31 06:52:47 +04:00
|
|
|
return
|
2017-09-30 06:32:43 +03:00
|
|
|
|
|
|
|
# seek to given layer (1..max)
|
2010-07-31 06:52:47 +04:00
|
|
|
try:
|
2019-03-21 16:28:20 +03:00
|
|
|
name, mode, bbox, tile = self.layers[layer - 1]
|
2010-07-31 06:52:47 +04:00
|
|
|
self.mode = mode
|
|
|
|
self.tile = tile
|
|
|
|
self.frame = layer
|
2022-04-13 02:54:17 +03:00
|
|
|
self.fp = self._fp
|
2010-07-31 06:52:47 +04:00
|
|
|
return name, bbox
|
2020-06-21 13:13:35 +03:00
|
|
|
except IndexError as e:
|
2022-12-22 00:51:35 +03:00
|
|
|
msg = "no such layer"
|
|
|
|
raise EOFError(msg) from e
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def tell(self):
|
|
|
|
# return layer number (0=image, 1..max=layers)
|
|
|
|
return self.frame
|
|
|
|
|
2021-04-01 17:41:46 +03:00
|
|
|
|
2021-03-07 21:04:25 +03:00
|
|
|
def _layerinfo(fp, ct_bytes):
|
2010-07-31 06:52:47 +04:00
|
|
|
# read layerinfo block
|
|
|
|
layers = []
|
2021-03-07 21:04:25 +03:00
|
|
|
|
|
|
|
def read(size):
|
|
|
|
return ImageFile._safe_read(fp, size)
|
|
|
|
|
2021-07-15 12:38:26 +03:00
|
|
|
ct = si16(read(2))
|
2021-03-07 21:04:25 +03:00
|
|
|
|
|
|
|
# sanity check
|
|
|
|
if ct_bytes < (abs(ct) * 20):
|
2022-12-22 00:51:35 +03:00
|
|
|
msg = "Layer block too short for number of layers requested"
|
|
|
|
raise SyntaxError(msg)
|
2021-03-07 21:04:25 +03:00
|
|
|
|
2022-04-10 19:50:49 +03:00
|
|
|
for _ in range(abs(ct)):
|
2010-07-31 06:52:47 +04:00
|
|
|
# bounding box
|
2014-08-26 17:47:10 +04:00
|
|
|
y0 = i32(read(4))
|
|
|
|
x0 = i32(read(4))
|
|
|
|
y1 = i32(read(4))
|
|
|
|
x1 = i32(read(4))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# image info
|
|
|
|
mode = []
|
2021-03-07 21:04:25 +03:00
|
|
|
ct_types = i16(read(2))
|
|
|
|
types = list(range(ct_types))
|
2012-07-29 17:48:40 +04:00
|
|
|
if len(types) > 4:
|
|
|
|
continue
|
|
|
|
|
2022-04-10 19:50:49 +03:00
|
|
|
for _ in types:
|
2010-07-31 06:52:47 +04:00
|
|
|
type = i16(read(2))
|
2012-07-29 17:48:40 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
if type == 65535:
|
|
|
|
m = "A"
|
|
|
|
else:
|
2012-07-29 17:48:40 +04:00
|
|
|
m = "RGBA"[type]
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
mode.append(m)
|
2022-01-24 03:06:41 +03:00
|
|
|
read(4) # size
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# 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:
|
2014-08-26 17:47:10 +04:00
|
|
|
mode = None # unknown
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# skip over blend flags and extra information
|
2018-10-18 21:49:14 +03:00
|
|
|
read(12) # filler
|
2010-07-31 06:52:47 +04:00
|
|
|
name = ""
|
2019-09-29 07:16:30 +03:00
|
|
|
size = i32(read(4)) # length of the extra data field
|
2010-07-31 06:52:47 +04:00
|
|
|
if size:
|
2021-03-07 21:04:25 +03:00
|
|
|
data_end = fp.tell() + size
|
2019-09-29 07:16:30 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
length = i32(read(4))
|
|
|
|
if length:
|
2021-03-07 21:04:25 +03:00
|
|
|
fp.seek(length - 16, io.SEEK_CUR)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
length = i32(read(4))
|
|
|
|
if length:
|
2021-03-07 21:04:25 +03:00
|
|
|
fp.seek(length, io.SEEK_CUR)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
length = i8(read(1))
|
2010-07-31 06:52:47 +04:00
|
|
|
if length:
|
2014-08-26 17:47:10 +04:00
|
|
|
# Don't know the proper encoding,
|
|
|
|
# Latin-1 should be a good guess
|
2019-03-21 16:28:20 +03:00
|
|
|
name = read(length).decode("latin-1", "replace")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2021-03-07 21:04:25 +03:00
|
|
|
fp.seek(data_end)
|
2010-07-31 06:52:47 +04:00
|
|
|
layers.append((name, mode, (x0, y0, x1, y1)))
|
|
|
|
|
|
|
|
# get tiles
|
2023-01-07 21:45:55 +03:00
|
|
|
for i, (name, mode, bbox) in enumerate(layers):
|
2010-07-31 06:52:47 +04:00
|
|
|
tile = []
|
|
|
|
for m in mode:
|
2021-03-07 21:04:25 +03:00
|
|
|
t = _maketile(fp, m, bbox, 1)
|
2010-07-31 06:52:47 +04:00
|
|
|
if t:
|
|
|
|
tile.extend(t)
|
|
|
|
layers[i] = name, mode, bbox, tile
|
|
|
|
|
|
|
|
return layers
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
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":
|
2014-05-10 08:36:15 +04:00
|
|
|
layer += ";I"
|
2010-07-31 06:52:47 +04:00
|
|
|
tile.append(("raw", bbox, offset, layer))
|
2019-03-21 16:28:20 +03:00
|
|
|
offset = offset + xsize * ysize
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
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":
|
2014-05-10 08:36:15 +04:00
|
|
|
layer += ";I"
|
2019-03-21 16:28:20 +03:00
|
|
|
tile.append(("packbits", bbox, offset, layer))
|
2010-07-31 06:52:47 +04:00
|
|
|
for y in range(ysize):
|
2020-05-08 21:11:58 +03:00
|
|
|
offset = offset + i16(bytecount, i)
|
2014-05-10 08:36:15 +04:00
|
|
|
i += 2
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
file.seek(offset)
|
|
|
|
|
|
|
|
if offset & 1:
|
2014-08-26 17:47:10 +04:00
|
|
|
read(1) # padding
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
return tile
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# registry
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_open(PsdImageFile.format, PsdImageFile, _accept)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_extension(PsdImageFile.format, ".psd")
|
2020-07-16 01:31:43 +03:00
|
|
|
|
|
|
|
Image.register_mime(PsdImageFile.format, "image/vnd.adobe.photoshop")
|