Skip to content

Instantly share code, notes, and snippets.

@yejianye
Created March 11, 2013 17:02
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 yejianye/5135725 to your computer and use it in GitHub Desktop.
Save yejianye/5135725 to your computer and use it in GitHub Desktop.
generate ios assets from retina version (image@2x.png -> image.png)
#!/usr/bin/env python
from PIL import Image
from argh import arg, dispatch_command
import os
def gen_assets(rootdir, force=False):
for root, dirs, files in os.walk(rootdir):
for d in dirs:
gen_thumb(os.path.join(root, d), force=force)
for f in files:
if f.endswith('@2x.png') and not f.endswith('-568h@2x.png'):
f = os.path.relpath(os.path.join(root, f))
name = f.replace('@2x.png', '.png')
if os.path.exists(name) and not force:
continue
print '%s -> %s' % (f, name)
img = Image.open(f)
width, height = img.size
img.thumbnail((width/2, height/2), Image.ANTIALIAS)
img.save(name, "PNG")
@dispatch_command
@arg('ROOTDIR', default='.', help='Root directory for searching assets')
@arg('--force', '-f', default=False, help='Force generating assets. Will overwrite existing files.')
def main(args):
gen_assets(args.ROOTDIR, force=args.force)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment