Skip to main content

Mike Kreuzer

A quick & dirty icon resizing script in Python

22 October 2015

I updated my phone from iOS 9.0.something to 9.1 in haste this morning, which meant I then had a lot of time on my hands today while I waited on Xcode 7.1 to download. Time to iron out some kinks in my workflows, including getting app icons together quickly for testing.

Acorn offers a full dictionary via JXA, & AppleScript if that's how you roll, but Acorn's sandboxed and can't read or write anywhere on its own, which for the purpose of creating copies of images in new sizes kind of defeats the point. My heart skipped a beat when I found Pixelmator has read & write access to the Pictures folder, but Pixelmator (apart from having an interface I enjoy a whole lot less than Acorn's) doesn't expose itself to scripting. So, to Python.

The irony of bypassing sandboxing via the totally insecure threat vector known as PIP is not lost on me. But this is a dev machine, and not being able to script apps on it is Just Plain Annoying™.

~~Update 15 September 2018: I removed the embedded gist that was here, the gist of course is still available.~~

Update November 2023: Here it is again, as I leave Github:

"""Quick & dirty icon image set - copy & resize base.png"""
from PIL import Image

SIZES = [(29, 1), (29, 2), (29, 3), (40, 1), (40, 2), (40, 3), (60, 2), (60, 3),
         (76, 1), (76, 2), (1024, 1)]

try:
    IMG = Image.open("base.png")
    for pixels, multiple in SIZES:
        side = pixels * multiple
        resized_img = IMG.resize((side, side))
        resized_img.save("icon-%(pixels)d@%(multiple)dx.png" %
                         {'pixels': pixels, "multiple": multiple})
except IOError as err:
    print("File error: {0}".format(err))
Tags: