Skip to content

Instantly share code, notes, and snippets.

@davols
Created March 6, 2016 10:56
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 davols/3bd1c874a351dcb95308 to your computer and use it in GitHub Desktop.
Save davols/3bd1c874a351dcb95308 to your computer and use it in GitHub Desktop.
Script to rename files (graphics) from iOS format to Android dpi folders.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import argparse
import os
import shutil
'''
call with a path that contains files that are named
according to iOS format. Renames and moves the files
into corresponding folder.
./arenamer.py -p files (-d optional, remove original file)
'''
def walkdirectory(directory, delete):
dirs = os.listdir(directory)
os.mkdir('%s/mdpi' % directory)
os.mkdir('%s/xhdpi' % directory)
os.mkdir('%s/xxhdpi' % directory)
for filen in dirs:
if filen.endswith('@1x.png'):
newDir = '%s/mdpi' % directory
newFileName = filen.replace('@1x.png', '.png')
shutil.copy('%s/%s' % (directory, filen),
'%s/%s' % (newDir, newFileName))
if delete:
os.remove('%s/%s' % (directory, filen))
elif filen.endswith('@2x.png'):
newFileName = filen.replace('@2x.png', '.png')
newDir = '%s/xhdpi' % directory
shutil.copy('%s/%s' % (directory, filen),
'%s/%s' % (newDir, newFileName))
if delete:
os.remove('%s/%s' % (directory, filen))
elif filen.endswith('@3x.png'):
newDir = '%s/xxhdpi' % directory
newFileName = filen.replace('@3x.png', '.png')
shutil.copy('%s/%s' % (directory, filen),
'%s/%s' % (newDir, newFileName))
if delete:
os.remove('%s/%s' % (directory, filen))
print 'Done'
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('-p', '--path', action='store', dest='path',
help='Path for the images')
parser.add_argument('-d', '--delete', dest='delete', action='store_true')
args = parser.parse_args()
if args.path is not None:
print 'Path: %s' % args.path
walkdirectory(args.path, args.delete)
else:
print 'You need to write a path'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment