Last active
June 28, 2019 10:04
-
-
Save markcastle/492465d5ac16ff67e56bbcb09684d246 to your computer and use it in GitHub Desktop.
Python script to create Xamarin Android Mipmap Resources from Image / Icon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Copyright 2017, 2018, 2019 Captive Reality Ltd | |
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is | |
hereby granted, provided that the above copyright notice and this permission notice appear in all copies. | |
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE | |
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE | |
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | |
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, | |
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
Source: https://gist.github.com/markcastle/492465d5ac16ff67e56bbcb09684d246 | |
Description: | |
Create Xamarin Android Mipmap Resources from Image / Icon | |
Installation: | |
You may need to install pip and then image like this... | |
python -m pip install -U pip | |
python -m pip install image | |
Usage: | |
Drop the resources.py folder into the same folder as your source Icon then run with... | |
python resources.py <SourceIconImageFile> | |
eg... | |
python resources.py ic_outbound.png | |
This will then create folders such as | |
Current Folder | |
+-- 24px | |
+-- ic_outbound.png | |
+-- 32px | |
+-- ic_outbound.png | |
+-- 512px | |
+-- ic_outbound.png | |
+-- mipmap-hdpi | |
+-- ic_outbound.png | |
+-- mipmap-ldpi | |
+-- ic_outbound.png | |
+-- mipmap-mdpi | |
+-- ic_outbound.png | |
+-- mipmap-xhdpi | |
+-- ic_outbound.png | |
+-- mipmap-xxhdpi | |
+-- ic_outbound.png | |
+-- mipmap-xxxhdpi | |
+-- ic_outbound.png | |
You can now drop these into your mipmap folder in Xamarin :-) | |
''' | |
import os, sys | |
from PIL import Image | |
# Creates a folder in the current directory | |
# Example | |
# createFolder('./data/') | |
def createFolder(directory): | |
try: | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
except OSError: | |
print ('Error: Creating directory. ' + directory) | |
# Resize an image to 'size x size' and output the result in a named folder. | |
# Example | |
# resizeImage('mipmap-xxxhdpi', 192, infile) | |
def resizeImage(foldername, size, infile): | |
# print ('Creating Folder...%s' % foldername) | |
createFolder('./%s/' % foldername) | |
outfile = './%s/%s' % (foldername, infile[0]) | |
# print ('outfile=%s' % outfile) | |
outsize = size, size | |
try: | |
im = Image.open(infile[0]) | |
im.thumbnail(outsize, Image.ANTIALIAS) | |
im.save(outfile, "png") | |
print ('icon saved:%s' % outfile) | |
except IOError: | |
print ('cannot create image for %s size:%s' % infile) | |
infile = sys.argv[1:] | |
resizeImage('512px', 512, infile) | |
resizeImage('mipmap-xxxhdpi', 192, infile) | |
resizeImage('mipmap-xxhdpi', 144, infile) | |
resizeImage('mipmap-xhdpi', 96, infile) | |
resizeImage('mipmap-hdpi', 72, infile) | |
resizeImage('mipmap-mdpi', 48, infile) | |
resizeImage('mipmap-ldpi', 36, infile) | |
resizeImage('32px', 32, infile) | |
resizeImage('24px', 24, infile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment