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
|
|
|
|
from ._binary import i8, i16be as i16, i32be as i32
|
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):
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
read = self.fp.read
|
|
|
|
|
|
|
|
#
|
|
|
|
# header
|
|
|
|
|
|
|
|
s = read(26)
|
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
|
|
|
if s[:4] != b"8BPS" or i16(s[4:]) != 1:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("not a PSD file")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
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:
|
2019-09-30 17:56:31 +03:00
|
|
|
raise OSError("not enough channels")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
self.mode = mode
|
2018-09-30 05:58:02 +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:
|
|
|
|
self.layers = _layerinfo(self.fp)
|
|
|
|
self.fp.seek(end)
|
|
|
|
|
|
|
|
#
|
|
|
|
# image descriptor
|
|
|
|
|
|
|
|
self.tile = _maketile(self.fp, mode, (0, 0) + self.size, channels)
|
|
|
|
|
|
|
|
# keep the file open
|
2019-03-31 16:02:22 +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
|
|
|
|
2015-04-15 03:43:05 +03:00
|
|
|
@property
|
|
|
|
def n_frames(self):
|
|
|
|
return len(self.layers)
|
|
|
|
|
2015-06-30 06:25:00 +03:00
|
|
|
@property
|
|
|
|
def is_animated(self):
|
|
|
|
return len(self.layers) > 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
|
2019-03-31 16:02:22 +03:00
|
|
|
self.fp = self.__fp
|
2010-07-31 06:52:47 +04:00
|
|
|
return name, bbox
|
|
|
|
except IndexError:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise EOFError("no such layer")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def tell(self):
|
|
|
|
# return layer number (0=image, 1..max=layers)
|
|
|
|
return self.frame
|
|
|
|
|
|
|
|
def load_prepare(self):
|
|
|
|
# create image memory if necessary
|
2019-03-21 16:28:20 +03:00
|
|
|
if not self.im or self.im.mode != self.mode or self.im.size != self.size:
|
2010-07-31 06:52:47 +04:00
|
|
|
self.im = Image.core.fill(self.mode, self.size, 0)
|
|
|
|
# create palette (optional)
|
|
|
|
if self.mode == "P":
|
|
|
|
Image.Image.load(self)
|
|
|
|
|
2019-03-31 16:02:22 +03:00
|
|
|
def _close__fp(self):
|
|
|
|
try:
|
|
|
|
if self.__fp != self.fp:
|
|
|
|
self.__fp.close()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
self.__fp = None
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def _layerinfo(file):
|
|
|
|
# read layerinfo block
|
|
|
|
layers = []
|
|
|
|
read = file.read
|
|
|
|
for i in range(abs(i16(read(2)))):
|
|
|
|
|
|
|
|
# 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
|
|
|
|
info = []
|
|
|
|
mode = []
|
2012-10-16 01:18:27 +04:00
|
|
|
types = list(range(i16(read(2))))
|
2012-07-29 17:48:40 +04:00
|
|
|
if len(types) > 4:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for i 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)
|
|
|
|
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:
|
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
|
|
|
combined = 0
|
|
|
|
if size:
|
2019-09-29 07:16:30 +03:00
|
|
|
data_end = file.tell() + size
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
length = i32(read(4))
|
|
|
|
if length:
|
2019-01-13 05:05:46 +03:00
|
|
|
file.seek(length - 16, io.SEEK_CUR)
|
2010-07-31 06:52:47 +04:00
|
|
|
combined += length + 4
|
|
|
|
|
|
|
|
length = i32(read(4))
|
|
|
|
if length:
|
2019-01-13 05:05:46 +03:00
|
|
|
file.seek(length, io.SEEK_CUR)
|
2010-07-31 06:52:47 +04:00
|
|
|
combined += length + 4
|
|
|
|
|
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
|
|
|
combined += length + 1
|
|
|
|
|
2019-09-29 07:16:30 +03:00
|
|
|
file.seek(data_end)
|
2010-07-31 06:52:47 +04:00
|
|
|
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
|
2014-05-10 08:36:15 +04:00
|
|
|
i += 1
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
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):
|
2019-03-21 16:28:20 +03:00
|
|
|
offset = offset + i16(bytecount[i : i + 2])
|
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")
|