2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
2016-09-23 14:12:03 +03:00
|
|
|
# macOS icns file decoder, based on icns.py by Bob Ippolito.
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# history:
|
|
|
|
# 2004-10-09 fl Turned into a PIL plugin; removed 2.3 dependencies.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2004 by Bob Ippolito.
|
|
|
|
# Copyright (c) 2004 by Secret Labs.
|
|
|
|
# Copyright (c) 2004 by Fredrik Lundh.
|
2014-03-24 20:10:23 +04:00
|
|
|
# Copyright (c) 2014 by Alastair Houghton.
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
2017-01-17 16:22:18 +03:00
|
|
|
from PIL import Image, ImageFile, PngImagePlugin
|
|
|
|
from PIL._binary import i8
|
2014-08-26 17:47:10 +04:00
|
|
|
import io
|
2015-04-23 13:25:45 +03:00
|
|
|
import os
|
|
|
|
import shutil
|
2014-08-26 17:47:10 +04:00
|
|
|
import struct
|
2015-04-23 13:25:45 +03:00
|
|
|
import sys
|
|
|
|
import tempfile
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
enable_jpeg2k = hasattr(Image.core, "jp2klib_version")
|
2014-03-28 13:09:55 +04:00
|
|
|
if enable_jpeg2k:
|
2014-03-28 13:30:16 +04:00
|
|
|
from PIL import Jpeg2KImagePlugin
|
2014-03-28 13:09:55 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
HEADERSIZE = 8
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def nextheader(fobj):
|
2019-03-21 16:28:20 +03:00
|
|
|
return struct.unpack(">4sI", fobj.read(HEADERSIZE))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2012-10-16 06:32:28 +04:00
|
|
|
def read_32t(fobj, start_length, size):
|
2010-07-31 06:52:47 +04:00
|
|
|
# The 128x128 icon seems to have an extra header for some reason.
|
2012-10-16 06:32:28 +04:00
|
|
|
(start, length) = start_length
|
2010-07-31 06:52:47 +04:00
|
|
|
fobj.seek(start)
|
|
|
|
sig = fobj.read(4)
|
2019-03-21 16:28:20 +03:00
|
|
|
if sig != b"\x00\x00\x00\x00":
|
|
|
|
raise SyntaxError("Unknown signature, expecting 0x00000000")
|
2012-10-16 06:32:28 +04:00
|
|
|
return read_32(fobj, (start + 4, length - 4), size)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2012-10-16 06:32:28 +04:00
|
|
|
def read_32(fobj, start_length, size):
|
2010-07-31 06:52:47 +04:00
|
|
|
"""
|
|
|
|
Read a 32bit RGB icon resource. Seems to be either uncompressed or
|
|
|
|
an RLE packbits-like scheme.
|
|
|
|
"""
|
2012-10-16 06:32:28 +04:00
|
|
|
(start, length) = start_length
|
2010-07-31 06:52:47 +04:00
|
|
|
fobj.seek(start)
|
2014-03-24 20:10:23 +04:00
|
|
|
pixel_size = (size[0] * size[2], size[1] * size[2])
|
|
|
|
sizesq = pixel_size[0] * pixel_size[1]
|
2010-07-31 06:52:47 +04:00
|
|
|
if length == sizesq * 3:
|
|
|
|
# uncompressed ("RGBRGBGB")
|
|
|
|
indata = fobj.read(length)
|
2014-03-24 20:10:23 +04:00
|
|
|
im = Image.frombuffer("RGB", pixel_size, indata, "raw", "RGB", 0, 1)
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
|
|
|
# decode image
|
2014-03-24 20:10:23 +04:00
|
|
|
im = Image.new("RGB", pixel_size, None)
|
2010-07-31 06:52:47 +04:00
|
|
|
for band_ix in range(3):
|
|
|
|
data = []
|
|
|
|
bytesleft = sizesq
|
|
|
|
while bytesleft > 0:
|
|
|
|
byte = fobj.read(1)
|
|
|
|
if not byte:
|
|
|
|
break
|
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
|
|
|
byte = i8(byte)
|
2010-07-31 06:52:47 +04:00
|
|
|
if byte & 0x80:
|
|
|
|
blocksize = byte - 125
|
|
|
|
byte = fobj.read(1)
|
|
|
|
for i in range(blocksize):
|
|
|
|
data.append(byte)
|
|
|
|
else:
|
|
|
|
blocksize = byte + 1
|
|
|
|
data.append(fobj.read(blocksize))
|
2014-05-10 08:36:15 +04:00
|
|
|
bytesleft -= blocksize
|
2010-07-31 06:52:47 +04:00
|
|
|
if bytesleft <= 0:
|
|
|
|
break
|
|
|
|
if bytesleft != 0:
|
2019-03-21 16:28:20 +03:00
|
|
|
raise SyntaxError("Error reading channel [%r left]" % bytesleft)
|
|
|
|
band = Image.frombuffer("L", pixel_size, b"".join(data), "raw", "L", 0, 1)
|
2010-07-31 06:52:47 +04:00
|
|
|
im.im.putband(band.im, band_ix)
|
|
|
|
return {"RGB": im}
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2012-10-16 06:32:28 +04:00
|
|
|
def read_mk(fobj, start_length, size):
|
2010-07-31 06:52:47 +04:00
|
|
|
# Alpha masks seem to be uncompressed
|
2015-04-24 11:24:52 +03:00
|
|
|
start = start_length[0]
|
2010-07-31 06:52:47 +04:00
|
|
|
fobj.seek(start)
|
2014-03-24 20:10:23 +04:00
|
|
|
pixel_size = (size[0] * size[2], size[1] * size[2])
|
|
|
|
sizesq = pixel_size[0] * pixel_size[1]
|
2019-03-21 16:28:20 +03:00
|
|
|
band = Image.frombuffer("L", pixel_size, fobj.read(sizesq), "raw", "L", 0, 1)
|
2010-07-31 06:52:47 +04:00
|
|
|
return {"A": band}
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2014-03-24 20:10:23 +04:00
|
|
|
def read_png_or_jpeg2000(fobj, start_length, size):
|
|
|
|
(start, length) = start_length
|
|
|
|
fobj.seek(start)
|
|
|
|
sig = fobj.read(12)
|
2019-03-21 16:28:20 +03:00
|
|
|
if sig[:8] == b"\x89PNG\x0d\x0a\x1a\x0a":
|
2014-03-24 20:10:23 +04:00
|
|
|
fobj.seek(start)
|
|
|
|
im = PngImagePlugin.PngImageFile(fobj)
|
|
|
|
return {"RGBA": im}
|
2019-03-21 16:28:20 +03:00
|
|
|
elif (
|
|
|
|
sig[:4] == b"\xff\x4f\xff\x51"
|
|
|
|
or sig[:4] == b"\x0d\x0a\x87\x0a"
|
|
|
|
or sig == b"\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a"
|
|
|
|
):
|
2014-03-28 13:09:55 +04:00
|
|
|
if not enable_jpeg2k:
|
2019-03-21 16:28:20 +03:00
|
|
|
raise ValueError(
|
|
|
|
"Unsupported icon subimage format (rebuild PIL "
|
|
|
|
"with JPEG 2000 support to fix this)"
|
|
|
|
)
|
2014-03-24 20:10:23 +04:00
|
|
|
# j2k, jpc or j2c
|
2014-03-24 21:24:49 +04:00
|
|
|
fobj.seek(start)
|
2014-03-24 22:04:37 +04:00
|
|
|
jp2kstream = fobj.read(length)
|
|
|
|
f = io.BytesIO(jp2kstream)
|
|
|
|
im = Jpeg2KImagePlugin.Jpeg2KImageFile(f)
|
2019-03-21 16:28:20 +03:00
|
|
|
if im.mode != "RGBA":
|
|
|
|
im = im.convert("RGBA")
|
2014-03-24 21:24:49 +04:00
|
|
|
return {"RGBA": im}
|
2014-03-28 13:09:55 +04:00
|
|
|
else:
|
2019-03-21 16:28:20 +03:00
|
|
|
raise ValueError("Unsupported icon subimage format")
|
2014-03-24 20:10:23 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2015-05-26 17:07:21 +03:00
|
|
|
class IcnsFile(object):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
SIZES = {
|
2019-03-21 16:28:20 +03:00
|
|
|
(512, 512, 2): [(b"ic10", read_png_or_jpeg2000)],
|
|
|
|
(512, 512, 1): [(b"ic09", read_png_or_jpeg2000)],
|
|
|
|
(256, 256, 2): [(b"ic14", read_png_or_jpeg2000)],
|
|
|
|
(256, 256, 1): [(b"ic08", read_png_or_jpeg2000)],
|
|
|
|
(128, 128, 2): [(b"ic13", read_png_or_jpeg2000)],
|
2014-03-24 20:10:23 +04:00
|
|
|
(128, 128, 1): [
|
2019-03-21 16:28:20 +03:00
|
|
|
(b"ic07", read_png_or_jpeg2000),
|
|
|
|
(b"it32", read_32t),
|
|
|
|
(b"t8mk", read_mk),
|
2010-07-31 06:52:47 +04:00
|
|
|
],
|
2019-03-21 16:28:20 +03:00
|
|
|
(64, 64, 1): [(b"icp6", read_png_or_jpeg2000)],
|
|
|
|
(32, 32, 2): [(b"ic12", read_png_or_jpeg2000)],
|
|
|
|
(48, 48, 1): [(b"ih32", read_32), (b"h8mk", read_mk)],
|
2014-03-24 20:10:23 +04:00
|
|
|
(32, 32, 1): [
|
2019-03-21 16:28:20 +03:00
|
|
|
(b"icp5", read_png_or_jpeg2000),
|
|
|
|
(b"il32", read_32),
|
|
|
|
(b"l8mk", read_mk),
|
2014-03-24 20:10:23 +04:00
|
|
|
],
|
2019-03-21 16:28:20 +03:00
|
|
|
(16, 16, 2): [(b"ic11", read_png_or_jpeg2000)],
|
2014-03-24 20:10:23 +04:00
|
|
|
(16, 16, 1): [
|
2019-03-21 16:28:20 +03:00
|
|
|
(b"icp4", read_png_or_jpeg2000),
|
|
|
|
(b"is32", read_32),
|
|
|
|
(b"s8mk", read_mk),
|
2010-07-31 06:52:47 +04:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, fobj):
|
|
|
|
"""
|
|
|
|
fobj is a file-like object as an icns resource
|
|
|
|
"""
|
|
|
|
# signature : (start, length)
|
|
|
|
self.dct = dct = {}
|
|
|
|
self.fobj = fobj
|
|
|
|
sig, filesize = nextheader(fobj)
|
2019-03-21 16:28:20 +03:00
|
|
|
if sig != b"icns":
|
|
|
|
raise SyntaxError("not an icns file")
|
2010-07-31 06:52:47 +04:00
|
|
|
i = HEADERSIZE
|
|
|
|
while i < filesize:
|
|
|
|
sig, blocksize = nextheader(fobj)
|
2014-08-07 03:42:43 +04:00
|
|
|
if blocksize <= 0:
|
2019-03-21 16:28:20 +03:00
|
|
|
raise SyntaxError("invalid block header")
|
2014-05-10 08:36:15 +04:00
|
|
|
i += HEADERSIZE
|
|
|
|
blocksize -= HEADERSIZE
|
2010-07-31 06:52:47 +04:00
|
|
|
dct[sig] = (i, blocksize)
|
2019-01-13 05:05:46 +03:00
|
|
|
fobj.seek(blocksize, io.SEEK_CUR)
|
2014-05-10 08:36:15 +04:00
|
|
|
i += blocksize
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def itersizes(self):
|
|
|
|
sizes = []
|
|
|
|
for size, fmts in self.SIZES.items():
|
|
|
|
for (fmt, reader) in fmts:
|
2012-10-16 01:18:27 +04:00
|
|
|
if fmt in self.dct:
|
2010-07-31 06:52:47 +04:00
|
|
|
sizes.append(size)
|
|
|
|
break
|
|
|
|
return sizes
|
|
|
|
|
|
|
|
def bestsize(self):
|
|
|
|
sizes = self.itersizes()
|
|
|
|
if not sizes:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("No 32bit icon resources found")
|
2010-07-31 06:52:47 +04:00
|
|
|
return max(sizes)
|
|
|
|
|
|
|
|
def dataforsize(self, size):
|
|
|
|
"""
|
|
|
|
Get an icon resource as {channel: array}. Note that
|
|
|
|
the arrays are bottom-up like windows bitmaps and will likely
|
|
|
|
need to be flipped or transposed in some way.
|
|
|
|
"""
|
|
|
|
dct = {}
|
|
|
|
for code, reader in self.SIZES[size]:
|
|
|
|
desc = self.dct.get(code)
|
|
|
|
if desc is not None:
|
|
|
|
dct.update(reader(self.fobj, desc, size))
|
|
|
|
return dct
|
|
|
|
|
|
|
|
def getimage(self, size=None):
|
|
|
|
if size is None:
|
|
|
|
size = self.bestsize()
|
2014-03-24 20:10:23 +04:00
|
|
|
if len(size) == 2:
|
|
|
|
size = (size[0], size[1], 1)
|
2010-07-31 06:52:47 +04:00
|
|
|
channels = self.dataforsize(size)
|
2014-03-24 20:10:23 +04:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
im = channels.get("RGBA", None)
|
2014-03-24 20:10:23 +04:00
|
|
|
if im:
|
|
|
|
return im
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
im = channels.get("RGB").copy()
|
|
|
|
try:
|
|
|
|
im.putalpha(channels["A"])
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
return im
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
##
|
|
|
|
# Image plugin for Mac OS icons.
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
class IcnsImageFile(ImageFile.ImageFile):
|
|
|
|
"""
|
2015-04-23 10:00:21 +03:00
|
|
|
PIL image support for Mac OS .icns files.
|
2010-07-31 06:52:47 +04:00
|
|
|
Chooses the best resolution, but will possibly load
|
|
|
|
a different size image if you mutate the size attribute
|
|
|
|
before calling 'load'.
|
|
|
|
|
|
|
|
The info dictionary has a key 'sizes' that is a list
|
|
|
|
of sizes that the icns file has.
|
|
|
|
"""
|
|
|
|
|
|
|
|
format = "ICNS"
|
|
|
|
format_description = "Mac OS icns resource"
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
self.icns = IcnsFile(self.fp)
|
2019-03-21 16:28:20 +03:00
|
|
|
self.mode = "RGBA"
|
|
|
|
self.info["sizes"] = self.icns.itersizes()
|
2014-03-24 20:10:23 +04:00
|
|
|
self.best_size = self.icns.bestsize()
|
2019-03-21 16:28:20 +03:00
|
|
|
self.size = (
|
|
|
|
self.best_size[0] * self.best_size[2],
|
|
|
|
self.best_size[1] * self.best_size[2],
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
# Just use this to see if it's loaded or not yet.
|
2019-03-21 16:28:20 +03:00
|
|
|
self.tile = ("",)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2018-09-30 05:58:02 +03:00
|
|
|
@property
|
|
|
|
def size(self):
|
|
|
|
return self._size
|
|
|
|
|
|
|
|
@size.setter
|
|
|
|
def size(self, value):
|
|
|
|
info_size = value
|
2019-03-21 16:28:20 +03:00
|
|
|
if info_size not in self.info["sizes"] and len(info_size) == 2:
|
2018-09-30 05:58:02 +03:00
|
|
|
info_size = (info_size[0], info_size[1], 1)
|
2019-03-21 16:28:20 +03:00
|
|
|
if (
|
|
|
|
info_size not in self.info["sizes"]
|
|
|
|
and len(info_size) == 3
|
|
|
|
and info_size[2] == 1
|
|
|
|
):
|
|
|
|
simple_sizes = [
|
|
|
|
(size[0] * size[2], size[1] * size[2]) for size in self.info["sizes"]
|
|
|
|
]
|
2018-09-30 05:58:02 +03:00
|
|
|
if value in simple_sizes:
|
2019-03-21 16:28:20 +03:00
|
|
|
info_size = self.info["sizes"][simple_sizes.index(value)]
|
|
|
|
if info_size not in self.info["sizes"]:
|
|
|
|
raise ValueError("This is not one of the allowed sizes of this image")
|
2018-09-30 05:58:02 +03:00
|
|
|
self._size = value
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def load(self):
|
2014-03-24 20:10:23 +04:00
|
|
|
if len(self.size) == 3:
|
|
|
|
self.best_size = self.size
|
2019-03-21 16:28:20 +03:00
|
|
|
self.size = (
|
|
|
|
self.best_size[0] * self.best_size[2],
|
|
|
|
self.best_size[1] * self.best_size[2],
|
|
|
|
)
|
2014-03-24 20:10:23 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
Image.Image.load(self)
|
|
|
|
if not self.tile:
|
|
|
|
return
|
|
|
|
self.load_prepare()
|
|
|
|
# This is likely NOT the best way to do it, but whatever.
|
2014-03-24 20:10:23 +04:00
|
|
|
im = self.icns.getimage(self.best_size)
|
|
|
|
|
|
|
|
# If this is a PNG or JPEG 2000, it won't be loaded yet
|
|
|
|
im.load()
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
self.im = im.im
|
|
|
|
self.mode = im.mode
|
|
|
|
self.size = im.size
|
2018-11-13 14:13:55 +03:00
|
|
|
if self._exclusive_fp:
|
|
|
|
self.fp.close()
|
2010-07-31 06:52:47 +04:00
|
|
|
self.fp = None
|
|
|
|
self.icns = None
|
|
|
|
self.tile = ()
|
|
|
|
self.load_end()
|
|
|
|
|
2015-04-23 13:25:45 +03:00
|
|
|
|
2015-04-12 05:58:46 +03:00
|
|
|
def _save(im, fp, filename):
|
2015-04-23 10:00:21 +03:00
|
|
|
"""
|
|
|
|
Saves the image as a series of PNG files,
|
|
|
|
that are then converted to a .icns file
|
2016-09-23 14:12:03 +03:00
|
|
|
using the macOS command line utility 'iconutil'.
|
2015-04-23 13:25:45 +03:00
|
|
|
|
2016-09-23 14:12:03 +03:00
|
|
|
macOS only.
|
2015-04-23 10:00:21 +03:00
|
|
|
"""
|
2015-09-02 16:48:22 +03:00
|
|
|
if hasattr(fp, "flush"):
|
2015-04-12 05:58:46 +03:00
|
|
|
fp.flush()
|
2015-04-23 13:25:45 +03:00
|
|
|
|
2015-04-12 05:58:46 +03:00
|
|
|
# create the temporary set of pngs
|
2019-03-21 16:28:20 +03:00
|
|
|
iconset = tempfile.mkdtemp(".iconset")
|
|
|
|
provided_images = {im.width: im for im in im.encoderinfo.get("append_images", [])}
|
2015-04-12 05:58:46 +03:00
|
|
|
last_w = None
|
2018-10-21 09:16:17 +03:00
|
|
|
second_path = None
|
2015-04-23 13:25:45 +03:00
|
|
|
for w in [16, 32, 128, 256, 512]:
|
2019-03-21 16:28:20 +03:00
|
|
|
prefix = "icon_{}x{}".format(w, w)
|
2015-04-23 13:25:45 +03:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
first_path = os.path.join(iconset, prefix + ".png")
|
2015-04-12 05:58:46 +03:00
|
|
|
if last_w == w:
|
2018-03-11 14:14:49 +03:00
|
|
|
shutil.copyfile(second_path, first_path)
|
2015-04-12 05:58:46 +03:00
|
|
|
else:
|
2018-03-19 11:38:07 +03:00
|
|
|
im_w = provided_images.get(w, im.resize((w, w), Image.LANCZOS))
|
|
|
|
im_w.save(first_path)
|
2015-04-23 13:25:45 +03:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
second_path = os.path.join(iconset, prefix + "@2x.png")
|
|
|
|
im_w2 = provided_images.get(w * 2, im.resize((w * 2, w * 2), Image.LANCZOS))
|
2018-03-19 11:38:07 +03:00
|
|
|
im_w2.save(second_path)
|
2019-03-21 16:28:20 +03:00
|
|
|
last_w = w * 2
|
2015-04-23 13:25:45 +03:00
|
|
|
|
2015-04-12 05:58:46 +03:00
|
|
|
# iconutil -c icns -o {} {}
|
|
|
|
from subprocess import Popen, PIPE, CalledProcessError
|
|
|
|
|
2015-04-23 13:25:45 +03:00
|
|
|
convert_cmd = ["iconutil", "-c", "icns", "-o", filename, iconset]
|
2019-03-21 16:28:20 +03:00
|
|
|
with open(os.devnull, "wb") as devnull:
|
2016-11-24 04:48:56 +03:00
|
|
|
convert_proc = Popen(convert_cmd, stdout=PIPE, stderr=devnull)
|
2015-04-12 05:58:46 +03:00
|
|
|
|
|
|
|
convert_proc.stdout.close()
|
|
|
|
|
|
|
|
retcode = convert_proc.wait()
|
|
|
|
|
|
|
|
# remove the temporary files
|
|
|
|
shutil.rmtree(iconset)
|
2015-04-23 13:25:45 +03:00
|
|
|
|
2015-04-12 05:58:46 +03:00
|
|
|
if retcode:
|
|
|
|
raise CalledProcessError(retcode, convert_cmd)
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
Image.register_open(IcnsImageFile.format, IcnsImageFile, lambda x: x[:4] == b"icns")
|
|
|
|
Image.register_extension(IcnsImageFile.format, ".icns")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
if sys.platform == "darwin":
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_save(IcnsImageFile.format, _save)
|
2015-04-12 05:58:46 +03:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_mime(IcnsImageFile.format, "image/icns")
|
2015-04-12 05:58:46 +03:00
|
|
|
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
if __name__ == "__main__":
|
2018-01-06 13:51:45 +03:00
|
|
|
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print("Syntax: python IcnsImagePlugin.py [file]")
|
|
|
|
sys.exit()
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
imf = IcnsImageFile(open(sys.argv[1], "rb"))
|
|
|
|
for size in imf.info["sizes"]:
|
2014-03-24 20:10:23 +04:00
|
|
|
imf.size = size
|
|
|
|
imf.load()
|
|
|
|
im = imf.im
|
2019-03-21 16:28:20 +03:00
|
|
|
im.save("out-%s-%s-%s.png" % size)
|
2018-01-06 13:40:12 +03:00
|
|
|
im = Image.open(sys.argv[1])
|
2010-07-31 06:52:47 +04:00
|
|
|
im.save("out.png")
|
2019-03-21 16:28:20 +03:00
|
|
|
if sys.platform == "windows":
|
2015-04-23 13:25:45 +03:00
|
|
|
os.startfile("out.png")
|