-
-
Save neoneo40/6b4268f5e4728b741e27 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python2 | |
# coding: utf-8 | |
from __future__ import print_function | |
from tempfile import NamedTemporaryFile | |
import os | |
import sys | |
from shutil import copyfile | |
from wand.image import Image | |
DIMENSIONS = { | |
'OSX': [ | |
('icon_16x16.png', 16), | |
('[email protected]', 16*2), | |
('icon_32x32.png', 32), | |
('[email protected]', 32*2), | |
('icon_128x128.png', 128), | |
('[email protected]', 128*2), | |
('icon_256x256.png', 256), | |
('[email protected]', 256*2), | |
('icon_512x512.png', 512), | |
('[email protected]', 512*2), | |
], | |
'IOS': [ | |
('icon-29.png', 29), | |
('[email protected]', 29*2), | |
('icon-40.png', 40), | |
('[email protected]', 40*2), | |
('[email protected]', 40*3), | |
('icon-60.png', 60), | |
('[email protected]', 60*2), | |
('[email protected]', 60*3), | |
('icon-72.png', 72), | |
('[email protected]', 72*2), | |
('icon-76.png', 76), | |
('[email protected]', 76*2), | |
('[email protected]', 76*3), | |
('icon-512.png', 512), | |
('[email protected]', 512*2), | |
('icon-small-50.png', 50), | |
('[email protected]', 50*2), | |
('[email protected]', 50*3), | |
('icon-small.png', 29), | |
('[email protected]', 29*2), | |
('[email protected]', 29*3), | |
('icon.png', 57), | |
('[email protected]', 57*2), | |
('iTunesArtwork.png', 512), | |
('[email protected]', 512*2), | |
], | |
'ANDROID': [ | |
('mipmap-ldpi/ic_launcher.png', 36), | |
('mipmap-mdpi/ic_launcher.png', 48), | |
('mipmap-hdpi/ic_launcher.png', 72), | |
('mipmap-xhdpi/ic_launcher.png', 96), | |
('mipmap-xxhdpi/ic_launcher.png', 144), | |
('mipmap-xxxhdpi/ic_launcher.png', 192), | |
('playstore-icon.png', 512), | |
], | |
} | |
cmd = ( | |
"convert -size {width}x{height} xc:none -fill white -draw " | |
"'roundRectangle 0,0 {width},{height} {round_size},{round_size}' " | |
"'{round_src_path}' -compose SrcIn -composite '{rounded_path}'") | |
def create(src_path): | |
src_path = os.path.abspath(src_path) | |
for t, dims in DIMENSIONS.items(): | |
for name, size in dims: | |
if isinstance(size, int): | |
width, height = (size, size) | |
else: | |
width, height = size | |
dest_path = os.path.join('app-icons', t, name) | |
dir_path = os.path.dirname(dest_path) | |
if not os.path.exists(dir_path): | |
os.makedirs(dir_path) | |
with Image(filename=src_path) as src_im: | |
src_im.strip() | |
src_im.resize(width, height) | |
src_im.save(filename=dest_path) | |
suffix = os.path.splitext(dest_path)[-1] | |
with NamedTemporaryFile(suffix=suffix) as rounded_f: | |
round_size = int(width * 0.15) | |
rounded_path = rounded_f.name | |
current_cmd = cmd.format(**{ | |
'width': width, | |
'height': height, | |
'round_size': round_size, | |
'round_src_path': dest_path, | |
'rounded_path': rounded_path, | |
}) | |
print(current_cmd) | |
os.system(current_cmd) | |
copyfile(rounded_path, dest_path) | |
print('created {}'.format(dest_path)) | |
if __name__ == '__main__': | |
try: | |
src_path = sys.argv[1] | |
except IndexError: | |
print('usage> {} <image-path>'.format(sys.argv[0]), file=sys.stderr) | |
sys.exit(1) | |
create(src_path) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment