Skip to content

Instantly share code, notes, and snippets.

@gelldur
Created June 27, 2016 14:45
Show Gist options
  • Save gelldur/8ad93c1df8836cc0925bd5073388e0af to your computer and use it in GitHub Desktop.
Save gelldur/8ad93c1df8836cc0925bd5073388e0af to your computer and use it in GitHub Desktop.
Script converting android res folder to ios assets. Eg. from res to Assets.xcassets
#!/usr/bin/python
import sys
import os
import json
from shutil import copyfile
print('Number of arguments:', len(sys.argv), 'arguments.')
if len(sys.argv) < 3:
print("Please pass android resource path and ios resource path")
print("eg. convertRes.py ./res ../Assets.xcassets/")
sys.exit(1)
print('Argument List:', str(sys.argv))
androidResFolder = sys.argv[1];
iosResFolder = sys.argv[2];
mdpiFolder = androidResFolder + "drawable-mdpi/"
xhdpiFolder = androidResFolder + "drawable-xhdpi/"
xxhdpiFolder = androidResFolder + "drawable-xxhdpi/"
# files = [f for f in os.listdir(mdpiFolder) if os.path.isfile(f)]
images = [];
images.extend(os.listdir(mdpiFolder))
images.extend(os.listdir(xhdpiFolder))
images.extend(os.listdir(xxhdpiFolder))
images = sorted(set(images));
for androidFile in images:
image = os.path.splitext(androidFile)[0]
imageFolder = iosResFolder + image + ".imageset"
if not os.path.exists(imageFolder):
os.makedirs(imageFolder)
contentsJson = {}
contentsJson['info'] = {}
contentsJson['info']['version'] = 1
contentsJson['info']['author'] = 'script'
images = [];
if os.path.exists(mdpiFolder + androidFile):
unit = {}
fileName = image + "-1" + os.path.splitext(androidFile)[1]
unit['idiom'] = 'universal'
unit['scale'] = '1x'
unit['filename'] = fileName
copyfile(mdpiFolder + androidFile, imageFolder + "/" + fileName)
images.append(unit)
if os.path.exists(xhdpiFolder + androidFile):
unit = {}
fileName = image + "-2" + os.path.splitext(androidFile)[1]
unit['idiom'] = 'universal'
unit['scale'] = '2x'
unit['filename'] = fileName
copyfile(xhdpiFolder + androidFile, imageFolder + "/" + fileName)
images.append(unit)
if os.path.exists(xxhdpiFolder + androidFile):
unit = {}
fileName = image + "-3" + os.path.splitext(androidFile)[1]
unit['idiom'] = 'universal'
unit['scale'] = '3x'
unit['filename'] = fileName
copyfile(xxhdpiFolder + androidFile, imageFolder + "/" + fileName)
images.append(unit)
contentsJson['images'] = images
output = open(imageFolder + "/Contents.json", 'w')
output.write(json.dumps(contentsJson, indent=3))
@radarlab-ian
Copy link

you saved my day

@nicmanzini
Copy link

does this thing exist in reverse?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment