Skip to content

Instantly share code, notes, and snippets.

@moltak
Last active December 18, 2015 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moltak/5742940 to your computer and use it in GitHub Desktop.
Save moltak/5742940 to your computer and use it in GitHub Desktop.
resource resizer.py
# 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