IcnsImagePlugin.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # macOS icns file decoder, based on icns.py by Bob Ippolito.
  6. #
  7. # history:
  8. # 2004-10-09 fl Turned into a PIL plugin; removed 2.3 dependencies.
  9. #
  10. # Copyright (c) 2004 by Bob Ippolito.
  11. # Copyright (c) 2004 by Secret Labs.
  12. # Copyright (c) 2004 by Fredrik Lundh.
  13. # Copyright (c) 2014 by Alastair Houghton.
  14. #
  15. # See the README file for information on usage and redistribution.
  16. #
  17. import io
  18. import os
  19. import shutil
  20. import struct
  21. import sys
  22. import tempfile
  23. from PIL import Image, ImageFile, PngImagePlugin
  24. from PIL._binary import i8
  25. enable_jpeg2k = hasattr(Image.core, "jp2klib_version")
  26. if enable_jpeg2k:
  27. from PIL import Jpeg2KImagePlugin
  28. HEADERSIZE = 8
  29. def nextheader(fobj):
  30. return struct.unpack(">4sI", fobj.read(HEADERSIZE))
  31. def read_32t(fobj, start_length, size):
  32. # The 128x128 icon seems to have an extra header for some reason.
  33. (start, length) = start_length
  34. fobj.seek(start)
  35. sig = fobj.read(4)
  36. if sig != b"\x00\x00\x00\x00":
  37. raise SyntaxError("Unknown signature, expecting 0x00000000")
  38. return read_32(fobj, (start + 4, length - 4), size)
  39. def read_32(fobj, start_length, size):
  40. """
  41. Read a 32bit RGB icon resource. Seems to be either uncompressed or
  42. an RLE packbits-like scheme.
  43. """
  44. (start, length) = start_length
  45. fobj.seek(start)
  46. pixel_size = (size[0] * size[2], size[1] * size[2])
  47. sizesq = pixel_size[0] * pixel_size[1]
  48. if length == sizesq * 3:
  49. # uncompressed ("RGBRGBGB")
  50. indata = fobj.read(length)
  51. im = Image.frombuffer("RGB", pixel_size, indata, "raw", "RGB", 0, 1)
  52. else:
  53. # decode image
  54. im = Image.new("RGB", pixel_size, None)
  55. for band_ix in range(3):
  56. data = []
  57. bytesleft = sizesq
  58. while bytesleft > 0:
  59. byte = fobj.read(1)
  60. if not byte:
  61. break
  62. byte = i8(byte)
  63. if byte & 0x80:
  64. blocksize = byte - 125
  65. byte = fobj.read(1)
  66. for i in range(blocksize):
  67. data.append(byte)
  68. else:
  69. blocksize = byte + 1
  70. data.append(fobj.read(blocksize))
  71. bytesleft -= blocksize
  72. if bytesleft <= 0:
  73. break
  74. if bytesleft != 0:
  75. raise SyntaxError("Error reading channel [%r left]" % bytesleft)
  76. band = Image.frombuffer("L", pixel_size, b"".join(data), "raw", "L", 0, 1)
  77. im.im.putband(band.im, band_ix)
  78. return {"RGB": im}
  79. def read_mk(fobj, start_length, size):
  80. # Alpha masks seem to be uncompressed
  81. start = start_length[0]
  82. fobj.seek(start)
  83. pixel_size = (size[0] * size[2], size[1] * size[2])
  84. sizesq = pixel_size[0] * pixel_size[1]
  85. band = Image.frombuffer("L", pixel_size, fobj.read(sizesq), "raw", "L", 0, 1)
  86. return {"A": band}
  87. def read_png_or_jpeg2000(fobj, start_length, size):
  88. (start, length) = start_length
  89. fobj.seek(start)
  90. sig = fobj.read(12)
  91. if sig[:8] == b"\x89PNG\x0d\x0a\x1a\x0a":
  92. fobj.seek(start)
  93. im = PngImagePlugin.PngImageFile(fobj)
  94. return {"RGBA": im}
  95. elif (
  96. sig[:4] == b"\xff\x4f\xff\x51"
  97. or sig[:4] == b"\x0d\x0a\x87\x0a"
  98. or sig == b"\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a"
  99. ):
  100. if not enable_jpeg2k:
  101. raise ValueError(
  102. "Unsupported icon subimage format (rebuild PIL "
  103. "with JPEG 2000 support to fix this)"
  104. )
  105. # j2k, jpc or j2c
  106. fobj.seek(start)
  107. jp2kstream = fobj.read(length)
  108. f = io.BytesIO(jp2kstream)
  109. im = Jpeg2KImagePlugin.Jpeg2KImageFile(f)
  110. if im.mode != "RGBA":
  111. im = im.convert("RGBA")
  112. return {"RGBA": im}
  113. else:
  114. raise ValueError("Unsupported icon subimage format")
  115. class IcnsFile(object):
  116. SIZES = {
  117. (512, 512, 2): [(b"ic10", read_png_or_jpeg2000)],
  118. (512, 512, 1): [(b"ic09", read_png_or_jpeg2000)],
  119. (256, 256, 2): [(b"ic14", read_png_or_jpeg2000)],
  120. (256, 256, 1): [(b"ic08", read_png_or_jpeg2000)],
  121. (128, 128, 2): [(b"ic13", read_png_or_jpeg2000)],
  122. (128, 128, 1): [
  123. (b"ic07", read_png_or_jpeg2000),
  124. (b"it32", read_32t),
  125. (b"t8mk", read_mk),
  126. ],
  127. (64, 64, 1): [(b"icp6", read_png_or_jpeg2000)],
  128. (32, 32, 2): [(b"ic12", read_png_or_jpeg2000)],
  129. (48, 48, 1): [(b"ih32", read_32), (b"h8mk", read_mk)],
  130. (32, 32, 1): [
  131. (b"icp5", read_png_or_jpeg2000),
  132. (b"il32", read_32),
  133. (b"l8mk", read_mk),
  134. ],
  135. (16, 16, 2): [(b"ic11", read_png_or_jpeg2000)],
  136. (16, 16, 1): [
  137. (b"icp4", read_png_or_jpeg2000),
  138. (b"is32", read_32),
  139. (b"s8mk", read_mk),
  140. ],
  141. }
  142. def __init__(self, fobj):
  143. """
  144. fobj is a file-like object as an icns resource
  145. """
  146. # signature : (start, length)
  147. self.dct = dct = {}
  148. self.fobj = fobj
  149. sig, filesize = nextheader(fobj)
  150. if sig != b"icns":
  151. raise SyntaxError("not an icns file")
  152. i = HEADERSIZE
  153. while i < filesize:
  154. sig, blocksize = nextheader(fobj)
  155. if blocksize <= 0:
  156. raise SyntaxError("invalid block header")
  157. i += HEADERSIZE
  158. blocksize -= HEADERSIZE
  159. dct[sig] = (i, blocksize)
  160. fobj.seek(blocksize, io.SEEK_CUR)
  161. i += blocksize
  162. def itersizes(self):
  163. sizes = []
  164. for size, fmts in self.SIZES.items():
  165. for (fmt, reader) in fmts:
  166. if fmt in self.dct:
  167. sizes.append(size)
  168. break
  169. return sizes
  170. def bestsize(self):
  171. sizes = self.itersizes()
  172. if not sizes:
  173. raise SyntaxError("No 32bit icon resources found")
  174. return max(sizes)
  175. def dataforsize(self, size):
  176. """
  177. Get an icon resource as {channel: array}. Note that
  178. the arrays are bottom-up like windows bitmaps and will likely
  179. need to be flipped or transposed in some way.
  180. """
  181. dct = {}
  182. for code, reader in self.SIZES[size]:
  183. desc = self.dct.get(code)
  184. if desc is not None:
  185. dct.update(reader(self.fobj, desc, size))
  186. return dct
  187. def getimage(self, size=None):
  188. if size is None:
  189. size = self.bestsize()
  190. if len(size) == 2:
  191. size = (size[0], size[1], 1)
  192. channels = self.dataforsize(size)
  193. im = channels.get("RGBA", None)
  194. if im:
  195. return im
  196. im = channels.get("RGB").copy()
  197. try:
  198. im.putalpha(channels["A"])
  199. except KeyError:
  200. pass
  201. return im
  202. ##
  203. # Image plugin for Mac OS icons.
  204. class IcnsImageFile(ImageFile.ImageFile):
  205. """
  206. PIL image support for Mac OS .icns files.
  207. Chooses the best resolution, but will possibly load
  208. a different size image if you mutate the size attribute
  209. before calling 'load'.
  210. The info dictionary has a key 'sizes' that is a list
  211. of sizes that the icns file has.
  212. """
  213. format = "ICNS"
  214. format_description = "Mac OS icns resource"
  215. def _open(self):
  216. self.icns = IcnsFile(self.fp)
  217. self.mode = "RGBA"
  218. self.info["sizes"] = self.icns.itersizes()
  219. self.best_size = self.icns.bestsize()
  220. self.size = (
  221. self.best_size[0] * self.best_size[2],
  222. self.best_size[1] * self.best_size[2],
  223. )
  224. @property
  225. def size(self):
  226. return self._size
  227. @size.setter
  228. def size(self, value):
  229. info_size = value
  230. if info_size not in self.info["sizes"] and len(info_size) == 2:
  231. info_size = (info_size[0], info_size[1], 1)
  232. if (
  233. info_size not in self.info["sizes"]
  234. and len(info_size) == 3
  235. and info_size[2] == 1
  236. ):
  237. simple_sizes = [
  238. (size[0] * size[2], size[1] * size[2]) for size in self.info["sizes"]
  239. ]
  240. if value in simple_sizes:
  241. info_size = self.info["sizes"][simple_sizes.index(value)]
  242. if info_size not in self.info["sizes"]:
  243. raise ValueError("This is not one of the allowed sizes of this image")
  244. self._size = value
  245. def load(self):
  246. if len(self.size) == 3:
  247. self.best_size = self.size
  248. self.size = (
  249. self.best_size[0] * self.best_size[2],
  250. self.best_size[1] * self.best_size[2],
  251. )
  252. Image.Image.load(self)
  253. if self.im and self.im.size == self.size:
  254. # Already loaded
  255. return
  256. self.load_prepare()
  257. # This is likely NOT the best way to do it, but whatever.
  258. im = self.icns.getimage(self.best_size)
  259. # If this is a PNG or JPEG 2000, it won't be loaded yet
  260. im.load()
  261. self.im = im.im
  262. self.mode = im.mode
  263. self.size = im.size
  264. self.load_end()
  265. def _save(im, fp, filename):
  266. """
  267. Saves the image as a series of PNG files,
  268. that are then converted to a .icns file
  269. using the macOS command line utility 'iconutil'.
  270. macOS only.
  271. """
  272. if hasattr(fp, "flush"):
  273. fp.flush()
  274. # create the temporary set of pngs
  275. iconset = tempfile.mkdtemp(".iconset")
  276. provided_images = {im.width: im for im in im.encoderinfo.get("append_images", [])}
  277. last_w = None
  278. second_path = None
  279. for w in [16, 32, 128, 256, 512]:
  280. prefix = "icon_{}x{}".format(w, w)
  281. first_path = os.path.join(iconset, prefix + ".png")
  282. if last_w == w:
  283. shutil.copyfile(second_path, first_path)
  284. else:
  285. im_w = provided_images.get(w, im.resize((w, w), Image.LANCZOS))
  286. im_w.save(first_path)
  287. second_path = os.path.join(iconset, prefix + "@2x.png")
  288. im_w2 = provided_images.get(w * 2, im.resize((w * 2, w * 2), Image.LANCZOS))
  289. im_w2.save(second_path)
  290. last_w = w * 2
  291. # iconutil -c icns -o {} {}
  292. from subprocess import Popen, PIPE, CalledProcessError
  293. convert_cmd = ["iconutil", "-c", "icns", "-o", filename, iconset]
  294. with open(os.devnull, "wb") as devnull:
  295. convert_proc = Popen(convert_cmd, stdout=PIPE, stderr=devnull)
  296. convert_proc.stdout.close()
  297. retcode = convert_proc.wait()
  298. # remove the temporary files
  299. shutil.rmtree(iconset)
  300. if retcode:
  301. raise CalledProcessError(retcode, convert_cmd)
  302. Image.register_open(IcnsImageFile.format, IcnsImageFile, lambda x: x[:4] == b"icns")
  303. Image.register_extension(IcnsImageFile.format, ".icns")
  304. if sys.platform == "darwin":
  305. Image.register_save(IcnsImageFile.format, _save)
  306. Image.register_mime(IcnsImageFile.format, "image/icns")
  307. if __name__ == "__main__":
  308. if len(sys.argv) < 2:
  309. print("Syntax: python IcnsImagePlugin.py [file]")
  310. sys.exit()
  311. imf = IcnsImageFile(open(sys.argv[1], "rb"))
  312. for size in imf.info["sizes"]:
  313. imf.size = size
  314. imf.load()
  315. im = imf.im
  316. im.save("out-%s-%s-%s.png" % size)
  317. im = Image.open(sys.argv[1])
  318. im.save("out.png")
  319. if sys.platform == "windows":
  320. os.startfile("out.png")