Skip to content

Instantly share code, notes, and snippets.

@dustin-graham
Created November 13, 2015 23:52
Show Gist options
  • Save dustin-graham/3b81a3e7c3c0872cab84 to your computer and use it in GitHub Desktop.
Save dustin-graham/3b81a3e7c3c0872cab84 to your computer and use it in GitHub Desktop.
imports image assets from a source directory into the res directory of an Android project. My designer gave me a directory of dpi-sized assets and this is an automated way to get them into the project quickly. The format this script expects is <nameoffile>_<densitymodifier>.<extension>. The files will be copied into the user-specified res folder…
import os, sys, shutil
legalResolutionModifiers = ["ldpi","mdpi","hdpi","xhdpi","xxhdpi","xxxhdpi"]
# check inputs
if len(sys.argv) != 3:
print "illegal usage"
sys.exit()
sourceFolder = sys.argv[1]
destination = sys.argv[2]
if not os.path.isdir(sourceFolder):
print "source dir invalid"
if not os.path.isdir(destination):
print "destination dir invalid"
files = []
for (dirpath, dirnames, filenames) in os.walk(sourceFolder):
files.extend(filenames)
break
for f in files:
title, ext = os.path.splitext(os.path.basename(f))
dpiModBegin = title.rfind("_")
rawTitle = title[:dpiModBegin]
densityMod = title[dpiModBegin+1:]
if densityMod in legalResolutionModifiers:
densityFolder = "drawable-"+densityMod
target = os.path.join(destination, densityFolder, rawTitle + ext)
source = os.path.join(sourceFolder, f)
shutil.copyfile(source,target)
# print source + " -> " + target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment