Last active
August 29, 2015 14:07
-
-
Save fredshonorio/3e7db450aed1c0736319 to your computer and use it in GitHub Desktop.
Converts images to mobile resolutions.
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
#!/bin/usr/python | |
# This script converts images to lower resolutions for android and ios. | |
# It assumes the image is in 4x/xxxhdpi | |
# Requires imagemagick. | |
import subprocess | |
import sys | |
import os | |
iosFolder = "ios" | |
androidFolder = "android" | |
androidFrac = { | |
"drawable-xxhdpi": '75', # 3/4 | |
"drawable-xhdpi": '50', # 2/4 | |
"drawable-hdpi": '37.5', # 1.5/4 | |
"drawable-mdpi": '25' # 1/4 | |
} | |
iosFrac = { | |
'@3x': '75', # 3/4 | |
'@2x': '50', # 2/4 | |
'@1x': '25' # 1/4 | |
} | |
def convert_file(fname): | |
for size, frac in iosFrac.iteritems(): | |
f, ext = os.path.splitext(fname) | |
# create dirs | |
if not os.path.exists(iosFolder): | |
os.makedirs(iosFolder) | |
# convert <f>.<ext> -resize <frac>% <iosFolder>/<f>-<size>.<ext> | |
subprocess.call([ | |
"convert", | |
fname, | |
"-resize", | |
frac + "%", | |
os.path.join(iosFolder, f + "-" + size + ext) | |
]) | |
for size, frac in androidFrac.iteritems(): | |
f, ext = os.path.splitext(fname) | |
# create dirs | |
dpath = os.path.join(androidFolder, size) | |
if not os.path.exists(dpath): | |
os.makedirs(dpath) | |
# convert <f>.<ext> -resize <frac>% <androidFolder>/<size>/<f>.<ext> | |
subprocess.call([ | |
"convert", | |
fname, | |
"-resize", | |
frac + "%", | |
os.path.join(dpath, fname) | |
]) | |
print "Processed '%s'" % fname | |
return | |
if __name__ == '__main__': | |
for f in sys.argv[1:]: | |
convert_file(f) | |
print "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment