Skip to content

Instantly share code, notes, and snippets.

@andrewmarmion
Last active January 31, 2017 21:02
Show Gist options
  • Select an option

  • Save andrewmarmion/33aa34b14f93a0023b5261aa1efb1fb0 to your computer and use it in GitHub Desktop.

Select an option

Save andrewmarmion/33aa34b14f93a0023b5261aa1efb1fb0 to your computer and use it in GitHub Desktop.
Sketch Export for Android
'''
A cheeky little python script that renames and correctly
puts exported image assets from Sketch into the correct
drawable folders for Android.
Images sizes supported 1x, 2x, 3x, 4x, 0.5x, 1.5x
The code is pretty simple so if you want to add support for
other sizes, it is easy to do.
To use:
Export all the image sizes from Sketch.
Make sure they have a suffix of @1x @2x etc.
Copy python file to folder where the images are contained.
Run the script.
Note: this will overwrite files without any warnings.
Use at your own risk.
Made by: Andrew Marmion January 2017
'''
import os
resolutions = ["mdpi","hdpi","xhdpi","xxhdpi","xxxhdpi"]
folder = "drawable-"
if not os.path.exists("drawable"):
os.makedirs("drawable")
for resolution in resolutions:
if not os.path.exists(folder + resolution):
print "making " + resolution
os.makedirs(folder + resolution)
for filename in os.listdir("."):
if filename.endswith(".png"):
print filename
location = ""
newFilename = ""
folder = "drawable-"
if "@1x" in filename:
newFilename = filename.replace("@1x","")
location = resolutions[0]
if "@" not in filename:
newFilename = filename
location = resolutions[0]
if "@1.5x" in filename:
newFilename = filename.replace("@1.5x","")
location = resolutions[1]
if "@2x" in filename:
newFilename = filename.replace("@2x","")
location = resolutions[2]
if "@3x" in filename:
newFilename = filename.replace("@3x","")
location = resolutions[3]
if "@4x" in filename:
newFilename = filename.replace("@4x","")
location = resolutions[4]
if "@0.5x" in filename:
newFilename = filename.replace("@0.5x","")
folder = "drawable"
newFile = folder + location +"/" + newFilename
os.rename(filename, newFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment