mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-10-29 23:17:49 +03:00 
			
		
		
		
	Merge remote-tracking branch 'radarhere/icns'
This commit is contained in:
		
						commit
						8c37960412
					
				|  | @ -200,12 +200,16 @@ attributes before loading the file:: | ||||||
| ICNS | ICNS | ||||||
| ^^^^ | ^^^^ | ||||||
| 
 | 
 | ||||||
| Pillow reads and (macOS only) writes macOS ``.icns`` files.  By default, the | Pillow reads and writes macOS ``.icns`` files.  By default, the | ||||||
| largest available icon is read, though you can override this by setting the | largest available icon is read, though you can override this by setting the | ||||||
| :py:attr:`~PIL.Image.Image.size` property before calling | :py:attr:`~PIL.Image.Image.size` property before calling | ||||||
| :py:meth:`~PIL.Image.Image.load`.  The :py:meth:`~PIL.Image.Image.open` method | :py:meth:`~PIL.Image.Image.load`.  The :py:meth:`~PIL.Image.Image.open` method | ||||||
| sets the following :py:attr:`~PIL.Image.Image.info` property: | sets the following :py:attr:`~PIL.Image.Image.info` property: | ||||||
| 
 | 
 | ||||||
|  | .. note:: | ||||||
|  | 
 | ||||||
|  |     Prior to version 7.2.0, Pillow could only write ICNS files on macOS. | ||||||
|  | 
 | ||||||
| **sizes** | **sizes** | ||||||
|     A list of supported sizes found in this icon file; these are a |     A list of supported sizes found in this icon file; these are a | ||||||
|     3-tuple, ``(width, height, scale)``, where ``scale`` is 2 for a retina |     3-tuple, ``(width, height, scale)``, where ``scale`` is 2 for a retina | ||||||
|  |  | ||||||
|  | @ -300,13 +300,12 @@ class IcnsImageFile(ImageFile.ImageFile): | ||||||
|         self.load_end() |         self.load_end() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def to_int(s): | def _to_int(s): | ||||||
|     b = s.encode("ascii") |     b = s.encode("ascii") | ||||||
|     return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3] |     return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3] | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| MAGIC = to_int("icns") | MAGIC = _to_int("icns") | ||||||
| HEADER_SIZE = 8 |  | ||||||
| TOC = "TOC " | TOC = "TOC " | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -326,32 +325,34 @@ def _save(im, fp, filename): | ||||||
|     file_size = 0 |     file_size = 0 | ||||||
|     entries = [] |     entries = [] | ||||||
|     provided_images = {im.width: im for im in im.encoderinfo.get("append_images", [])} |     provided_images = {im.width: im for im in im.encoderinfo.get("append_images", [])} | ||||||
|     for index, s in enumerate(sizes): |     temp_sizes = {s: io.BytesIO() for s in set(sizes)} | ||||||
|         temp = io.BytesIO() |     for s, temp in temp_sizes.items(): | ||||||
|         nb = provided_images[s] if s in provided_images else im.resize((s, s)) |         nb = provided_images[s] if s in provided_images else im.resize((s, s)) | ||||||
|         nb.save(temp, "png") |         nb.save(temp, "png") | ||||||
|  |     for index, s in enumerate(sizes): | ||||||
|  |         temp = temp_sizes[s] | ||||||
|         file_size += len(temp.getvalue()) |         file_size += len(temp.getvalue()) | ||||||
|         entries.append( |         entries.append( | ||||||
|             {"type": size_str[index], "size": len(temp.getvalue()), "stream": temp} |             {"type": size_str[index], "size": len(temp.getvalue()), "stream": temp} | ||||||
|         ) |         ) | ||||||
| 
 | 
 | ||||||
|     # Header |     # Header | ||||||
|     fp.write(struct.pack("i", MAGIC)[::-1]) |     fp.write(struct.pack("<i", MAGIC)[::-1]) | ||||||
|     fp.write(struct.pack("i", file_size)[::-1]) |     fp.write(struct.pack("<i", file_size)[::-1]) | ||||||
| 
 | 
 | ||||||
|     # TOC |     # TOC | ||||||
|     toc_size = HEADER_SIZE + (len(entries) * HEADER_SIZE) |     toc_size = HEADERSIZE + (len(entries) * HEADERSIZE) | ||||||
|     fp.write(struct.pack("i", to_int(TOC))[::-1]) |     fp.write(struct.pack("<i", _to_int(TOC))[::-1]) | ||||||
|     fp.write(struct.pack("i", toc_size)[::-1]) |     fp.write(struct.pack("<i", toc_size)[::-1]) | ||||||
|     for e in entries: |     for e in entries: | ||||||
|         fp.write(struct.pack("i", to_int(e.get("type")))[::-1]) |         fp.write(struct.pack("<i", _to_int(e["type"]))[::-1]) | ||||||
|         fp.write(struct.pack("i", HEADER_SIZE + e.get("size"))[::-1]) |         fp.write(struct.pack("<i", HEADERSIZE + e["size"])[::-1]) | ||||||
| 
 | 
 | ||||||
|     # Data |     # Data | ||||||
|     for index, e in enumerate(entries): |     for index, e in enumerate(entries): | ||||||
|         fp.write(struct.pack("i", to_int(e.get("type")))[::-1]) |         fp.write(struct.pack("<i", _to_int(e["type"]))[::-1]) | ||||||
|         fp.write(struct.pack("i", HEADER_SIZE + e.get("size"))[::-1]) |         fp.write(struct.pack("<i", HEADERSIZE + e["size"])[::-1]) | ||||||
|         fp.write(e.get("stream").getvalue()) |         fp.write(e["stream"].getvalue()) | ||||||
| 
 | 
 | ||||||
|     fp.flush() |     fp.flush() | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user