Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. import os, sys, fnmatch, json, shutil
  2. from PIL import Image
  3.  
  4. class XCAssetImage:
  5. def __init__(self,size,scale,idiom,filename):
  6. self.size = size
  7. self.scale = scale
  8. self.idiom = idiom
  9. self.filename = filename+'_'+str(self.size)+'_'+str(self.scale)+'x.png'
  10.  
  11. def to_json(self):
  12. jsonDict = {}
  13. jsonDict['size'] = str(self.size)+'x'+str(self.size)
  14. jsonDict['idiom'] = self.idiom
  15. jsonDict['filename'] = self.filename
  16. jsonDict['scale'] = str(self.scale)+'x'
  17. # print jsonDict
  18. return jsonDict
  19.  
  20. def emptyDir(directory):
  21. for root, dirs, files in os.walk(directory):
  22. for f in files:
  23. os.unlink(os.path.join(root, f))
  24. for d in dirs:
  25. shutil.rmtree(os.path.join(root, d))
  26.  
  27. def findFiles(projectPath):
  28.  
  29. pattern = 'AppIcon.appiconset'
  30.  
  31. for root, dirnames, filenames in os.walk(projectPath):
  32.  
  33. for directory in dirnames:
  34. if pattern in directory:
  35. print 'found file'
  36. fullPath = os.path.join(root, directory)
  37. print fullPath
  38. return fullPath
  39.  
  40. def makeImage(source_img,asset):
  41. try:
  42. pixels = asset.size*asset.scale
  43. size = (pixels,pixels)
  44. source_img.thumbnail(size)
  45. source_img.save(asset.filename, "PNG")
  46. successStr = 'created image for size ' + str(size)
  47. print successStr
  48. except IOError:
  49. errStr = 'cannot create image for size ' + str(size)
  50. print errStr
  51. return
  52.  
  53. def generate_assets(filename, iconsetPath):
  54.  
  55. contentsPath = iconsetPath + '/Contents.json'
  56.  
  57. print contentsPath
  58.  
  59. contents = open(contentsPath, 'r')
  60.  
  61. spec = json.load(contents)
  62.  
  63. prettyPrint(spec)
  64.  
  65. assets = []
  66.  
  67. for imageObject in spec['images']:
  68.  
  69. size = imageObject['size']
  70. size = size.split('x').pop(0)
  71.  
  72. scale = imageObject['scale']
  73. scale = scale.split('x').pop(0)
  74.  
  75. asset = XCAssetImage(int(size),int(scale),imageObject['idiom'],filename)
  76.  
  77. assets.append(asset)
  78.  
  79. return assets
  80.  
  81. def prettyPrint(jsonObj):
  82. print json.dumps(jsonObj,sort_keys=True,indent=4,separators=(',', ': '))
  83.  
  84.  
  85. def writeContents(assets):
  86.  
  87. jsonDict = {}
  88.  
  89. jsonDict['info'] = {
  90. 'version' : 1,
  91. 'author' : 'xcode'
  92. }
  93.  
  94. images = []
  95. for a in assets:
  96. images.append(a.to_json())
  97.  
  98. jsonDict['images'] = images
  99.  
  100. prettyPrint(jsonDict)
  101.  
  102. with open('Contents.json', 'w') as outfile:
  103. json.dump(jsonDict, outfile)
  104.  
  105. def main():
  106.  
  107. #intended invocation:
  108. #python make_images.py <imagename (relative to script location)> <project path>
  109. #python make_images.py DBAppIcon2.png /Users/niko/Documents/DuncansBurgers
  110. argsList = sys.argv
  111.  
  112. if len(argsList) < 3:
  113. exit('not enough args')
  114.  
  115. path = argsList.pop()
  116. imgName = argsList.pop()
  117. fileName = os.path.splitext(imgName)[0]
  118.  
  119. newDir = findFiles(path)
  120.  
  121. assets = generate_assets(fileName, newDir)
  122.  
  123. emptyDir(newDir)
  124.  
  125. source_img = Image.open(imgName)
  126.  
  127. os.chdir(newDir)
  128.  
  129. for a in assets:
  130. img = source_img.copy()
  131. makeImage(img,a)
  132.  
  133.  
  134. writeContents(assets)
  135.  
  136. if __name__ == '__main__':
  137. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement