Skip to content

Instantly share code, notes, and snippets.

@nuhil
Created April 29, 2020 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuhil/8b1d5b2693c7c857dd0c26659d7b5e80 to your computer and use it in GitHub Desktop.
Save nuhil/8b1d5b2693c7c857dd0c26659d7b5e80 to your computer and use it in GitHub Desktop.
# Run this script using following command
# $ python generate-iconset.py icon.png
# Then execute the following command to create .icns
# $ iconutil -c icns icon.iconset
import os
import sys
import pathlib
import subprocess
if len(sys.argv) < 2:
print("No path to original / hi-res icon provided")
raise SystemExit
if len(sys.argv) > 2:
print("Too many arguments")
raise SystemExit
originalPicture = sys.argv[1]
if not (os.path.isfile(originalPicture)):
print(f"There is no such file: {sys.argv[1]}")
raise SystemExit
fname = pathlib.Path(originalPicture).stem
ext = pathlib.Path(originalPicture).suffix
destDir = pathlib.Path(originalPicture).parent
iconsetDir = os.path.join(destDir, f"{fname}.iconset")
if not (os.path.exists(iconsetDir)):
pathlib.Path(iconsetDir).mkdir(parents=False, exist_ok=True)
class IconParameters():
width = 0
scale = 1
def __init__(self,width,scale):
self.width = width
self.scale = scale
def getIconName(self):
if self.scale != 1:
return f"icon_{self.width}x{self.width}{ext}"
else:
return f"icon_{self.width//2}x{self.width//2}@2x{ext}"
# https://developer.apple.com/design/human-interface-guidelines/macos/icons-and-images/app-icon#app-icon-sizes
ListOfIconParameters = [
IconParameters(16, 1),
IconParameters(16, 2),
IconParameters(32, 1),
IconParameters(32, 2),
IconParameters(64, 1),
IconParameters(64, 2),
IconParameters(128, 1),
IconParameters(128, 2),
IconParameters(256, 1),
IconParameters(256, 2),
IconParameters(512, 1),
IconParameters(512, 2),
IconParameters(1024, 1),
IconParameters(1024, 2)
]
# generate iconset
for ip in ListOfIconParameters:
subprocess.call(["sips", "-z", str(ip.width), str(ip.width), originalPicture, "--out", os.path.join(iconsetDir, ip.getIconName())])
#print(f"Generated {ip.getIconName()}")
# convert iconset to icns file
subprocess.call(["iconutil", "-c", "icns", iconsetDir, "-o", os.path.join(destDir, f"{fname}.icns")])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment