You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
7.4 KiB

4 years ago
  1. #
  2. # The Python Imaging Library
  3. # $Id$
  4. #
  5. # Adobe PSD 2.5/3.0 file handling
  6. #
  7. # History:
  8. # 1995-09-01 fl Created
  9. # 1997-01-03 fl Read most PSD images
  10. # 1997-01-18 fl Fixed P and CMYK support
  11. # 2001-10-21 fl Added seek/tell support (for layers)
  12. #
  13. # Copyright (c) 1997-2001 by Secret Labs AB.
  14. # Copyright (c) 1995-2001 by Fredrik Lundh
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18. __version__ = "0.4"
  19. from . import Image, ImageFile, ImagePalette
  20. from ._binary import i8, i16be as i16, i32be as i32
  21. MODES = {
  22. # (photoshop mode, bits) -> (pil mode, required channels)
  23. (0, 1): ("1", 1),
  24. (0, 8): ("L", 1),
  25. (1, 8): ("L", 1),
  26. (2, 8): ("P", 1),
  27. (3, 8): ("RGB", 3),
  28. (4, 8): ("CMYK", 4),
  29. (7, 8): ("L", 1), # FIXME: multilayer
  30. (8, 8): ("L", 1), # duotone
  31. (9, 8): ("LAB", 3)
  32. }
  33. # --------------------------------------------------------------------.
  34. # read PSD images
  35. def _accept(prefix):
  36. return prefix[:4] == b"8BPS"
  37. ##
  38. # Image plugin for Photoshop images.
  39. class PsdImageFile(ImageFile.ImageFile):
  40. format = "PSD"
  41. format_description = "Adobe Photoshop"
  42. def _open(self):
  43. read = self.fp.read
  44. #
  45. # header
  46. s = read(26)
  47. if s[:4] != b"8BPS" or i16(s[4:]) != 1:
  48. raise SyntaxError("not a PSD file")
  49. psd_bits = i16(s[22:])
  50. psd_channels = i16(s[12:])
  51. psd_mode = i16(s[24:])
  52. mode, channels = MODES[(psd_mode, psd_bits)]
  53. if channels > psd_channels:
  54. raise IOError("not enough channels")
  55. self.mode = mode
  56. self._size = i32(s[18:]), i32(s[14:])
  57. #
  58. # color mode data
  59. size = i32(read(4))
  60. if size:
  61. data = read(size)
  62. if mode == "P" and size == 768:
  63. self.palette = ImagePalette.raw("RGB;L", data)
  64. #
  65. # image resources
  66. self.resources = []
  67. size = i32(read(4))
  68. if size:
  69. # load resources
  70. end = self.fp.tell() + size
  71. while self.fp.tell() < end:
  72. signature = read(4)
  73. id = i16(read(2))
  74. name = read(i8(read(1)))
  75. if not (len(name) & 1):
  76. read(1) # padding
  77. data = read(i32(read(4)))
  78. if (len(data) & 1):
  79. read(1) # padding
  80. self.resources.append((id, name, data))
  81. if id == 1039: # ICC profile
  82. self.info["icc_profile"] = data
  83. #
  84. # layer and mask information
  85. self.layers = []
  86. size = i32(read(4))
  87. if size:
  88. end = self.fp.tell() + size
  89. size = i32(read(4))
  90. if size:
  91. self.layers = _layerinfo(self.fp)
  92. self.fp.seek(end)
  93. #
  94. # image descriptor
  95. self.tile = _maketile(self.fp, mode, (0, 0) + self.size, channels)
  96. # keep the file open
  97. self._fp = self.fp
  98. self.frame = 1
  99. self._min_frame = 1
  100. @property
  101. def n_frames(self):
  102. return len(self.layers)
  103. @property
  104. def is_animated(self):
  105. return len(self.layers) > 1
  106. def seek(self, layer):
  107. if not self._seek_check(layer):
  108. return
  109. # seek to given layer (1..max)
  110. try:
  111. name, mode, bbox, tile = self.layers[layer-1]
  112. self.mode = mode
  113. self.tile = tile
  114. self.frame = layer
  115. self.fp = self._fp
  116. return name, bbox
  117. except IndexError:
  118. raise EOFError("no such layer")
  119. def tell(self):
  120. # return layer number (0=image, 1..max=layers)
  121. return self.frame
  122. def load_prepare(self):
  123. # create image memory if necessary
  124. if not self.im or\
  125. self.im.mode != self.mode or self.im.size != self.size:
  126. self.im = Image.core.fill(self.mode, self.size, 0)
  127. # create palette (optional)
  128. if self.mode == "P":
  129. Image.Image.load(self)
  130. def _layerinfo(file):
  131. # read layerinfo block
  132. layers = []
  133. read = file.read
  134. for i in range(abs(i16(read(2)))):
  135. # bounding box
  136. y0 = i32(read(4))
  137. x0 = i32(read(4))
  138. y1 = i32(read(4))
  139. x1 = i32(read(4))
  140. # image info
  141. info = []
  142. mode = []
  143. types = list(range(i16(read(2))))
  144. if len(types) > 4:
  145. continue
  146. for i in types:
  147. type = i16(read(2))
  148. if type == 65535:
  149. m = "A"
  150. else:
  151. m = "RGBA"[type]
  152. mode.append(m)
  153. size = i32(read(4))
  154. info.append((m, size))
  155. # figure out the image mode
  156. mode.sort()
  157. if mode == ["R"]:
  158. mode = "L"
  159. elif mode == ["B", "G", "R"]:
  160. mode = "RGB"
  161. elif mode == ["A", "B", "G", "R"]:
  162. mode = "RGBA"
  163. else:
  164. mode = None # unknown
  165. # skip over blend flags and extra information
  166. filler = read(12)
  167. name = ""
  168. size = i32(read(4))
  169. combined = 0
  170. if size:
  171. length = i32(read(4))
  172. if length:
  173. mask_y = i32(read(4))
  174. mask_x = i32(read(4))
  175. mask_h = i32(read(4)) - mask_y
  176. mask_w = i32(read(4)) - mask_x
  177. file.seek(length - 16, 1)
  178. combined += length + 4
  179. length = i32(read(4))
  180. if length:
  181. file.seek(length, 1)
  182. combined += length + 4
  183. length = i8(read(1))
  184. if length:
  185. # Don't know the proper encoding,
  186. # Latin-1 should be a good guess
  187. name = read(length).decode('latin-1', 'replace')
  188. combined += length + 1
  189. file.seek(size - combined, 1)
  190. layers.append((name, mode, (x0, y0, x1, y1)))
  191. # get tiles
  192. i = 0
  193. for name, mode, bbox in layers:
  194. tile = []
  195. for m in mode:
  196. t = _maketile(file, m, bbox, 1)
  197. if t:
  198. tile.extend(t)
  199. layers[i] = name, mode, bbox, tile
  200. i += 1
  201. return layers
  202. def _maketile(file, mode, bbox, channels):
  203. tile = None
  204. read = file.read
  205. compression = i16(read(2))
  206. xsize = bbox[2] - bbox[0]
  207. ysize = bbox[3] - bbox[1]
  208. offset = file.tell()
  209. if compression == 0:
  210. #
  211. # raw compression
  212. tile = []
  213. for channel in range(channels):
  214. layer = mode[channel]
  215. if mode == "CMYK":
  216. layer += ";I"
  217. tile.append(("raw", bbox, offset, layer))
  218. offset = offset + xsize*ysize
  219. elif compression == 1:
  220. #
  221. # packbits compression
  222. i = 0
  223. tile = []
  224. bytecount = read(channels * ysize * 2)
  225. offset = file.tell()
  226. for channel in range(channels):
  227. layer = mode[channel]
  228. if mode == "CMYK":
  229. layer += ";I"
  230. tile.append(
  231. ("packbits", bbox, offset, layer)
  232. )
  233. for y in range(ysize):
  234. offset = offset + i16(bytecount[i:i+2])
  235. i += 2
  236. file.seek(offset)
  237. if offset & 1:
  238. read(1) # padding
  239. return tile
  240. # --------------------------------------------------------------------
  241. # registry
  242. Image.register_open(PsdImageFile.format, PsdImageFile, _accept)
  243. Image.register_extension(PsdImageFile.format, ".psd")