Bartek Wilczynski bio photo

Bartek Wilczynski

IT consultant and entrepreneur with over 10 years of experience in a variety of medium-size line of business applications, mostly in .NET technology.

Email Twitter LinkedIn Youtube

I don't have a new iPhone 4 yet, but I (and probably most of you) do want to support a new Retina display in your apps. In order for your apps to squeeze the most out of a new Apple device you would need to include a second set of all your images, which are twice the size of images from previous iPhone / iPod devices.

Fortunately, for developers, it's quite easy - you just need to prepare a high-res version of your images and append a file name with @2x, for example if your low-res image is named logo.png a high-res version should be named logo@2x.png. iOS4 should automatically pick up the one that is appropriate for your device.

Pretty much clear, right? But... how can you ensure that your graphic designer doesn't screw things up ;) and provides you consistent images according to these requirements... I am a big fan of automating things and avoiding any repetitive, boring task that are error-prone. Instead I created a simple Python script that is looking for all high-res images in my folder and simply scale it down, simple huh? Not sure if it works cause I didn't have a chance to test it on a real device, but I am pretty sure it will work like a charm :) If you like this solution and it is working fine or you find any problems I would really appreciate if you send me a feedback in a comment. Thanks.

Following is a source code, put it in any *.py file you like and chmod +x it to make it executable.

#!/usr/bin/python

import Image
import glob, os
import re

for infile in glob.glob("**/*@2x.png"):
	m = re.search("^(.*)@2x\.(.*)$", infile)
	file = m.group(1)
	ext = m.group(2)

	outfile = file + ".png"
	print "Converting {0} to {1}".format(infile, outfile)

	im = Image.open(infile)
	width, height = im.size
	resized = im.resize((width / 2, height / 2), Image.ANTIALIAS)
	resized.save(outfile, "PNG")