Last active
December 18, 2015 06:59
-
-
Save moltak/5742940 to your computer and use it in GitHub Desktop.
resource resizer.py
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
# code updated 2014.05.16 | |
from os import listdir | |
from os.path import isfile, join | |
import os | |
import shutil | |
from PIL import Image | |
import string | |
xxhdpi = './drawable-xxhdpi/' | |
xhdpi = './drawable-xhdpi/' | |
hdpi = './drawable-hdpi/' | |
mdpi = './drawable-mdpi/' | |
def makeDpiFolder(dpi): | |
if os.path.exists(dpi) == False: | |
os.makedirs(dpi) | |
def resizeFile(image_file, dpi, ratio): | |
print 'resizing ' + image_file | |
im = Image.open(xxhdpi + image_file) | |
width = int(im.size[0] * ratio) | |
height = int(im.size[1] * ratio) | |
newim = im.resize((width, height), Image.ANTIALIAS) | |
outfile = dpi + image_file | |
newim.save(outfile, 'PNG') | |
printCopySummary(im, width, height, outfile) | |
def printCopySummary(im, width, height, outfile): | |
print ("%s w: %d->%d h: %d->%d" % (outfile, im.size[0], width, im.size[1], height)) | |
makeDpiFolder(xhdpi) | |
makeDpiFolder(hdpi) | |
makeDpiFolder(mdpi) | |
onlyfiles = [f for f in listdir(xxhdpi) if isfile(join(xxhdpi,f))] | |
copycount = 0 | |
for f in onlyfiles: | |
file_extension = f.split('.')[1] | |
if (string.upper(file_extension) == 'PNG') | (file_extension == '9'): | |
resizeFile(f, xhdpi, 0.66) | |
resizeFile(f, hdpi, 0.5) | |
resizeFile(f, mdpi, 0.33) | |
copycount += 1 | |
print ('\ndone: %d files' % copycount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment