Skip to content

Instantly share code, notes, and snippets.

@corbett
Created September 12, 2012 08:18
Show Gist options
  • Save corbett/3705171 to your computer and use it in GitHub Desktop.
Save corbett/3705171 to your computer and use it in GitHub Desktop.
resize images for Android
import os
import glob
from PIL import Image
from math import ceil
xhdpi2hdpi=0.75
hdpi2mdpi=2/3.
mdpi2ldpi=0.75
for asset in glob.glob('drawable-xhdpi/*png'):
img = Image.open(asset)
xhdpi = img.size
hdpi = [ceil(xhdpi2hdpi*x) for x in xhdpi]
mdpi = [ceil(hdpi2mdpi*x) for x in hdpi]
ldpi = [ceil(mdpi2ldpi*x) for x in mdpi]
asset_name=os.path.basename(asset)
for convert_factor,dir_name in [(hdpi,'hdpi'),(mdpi,'mdpi'),(ldpi,'ldpi')]:
os.system("convert -resize %dx%d %s drawable-%s/%s" % \
(convert_factor[0],convert_factor[1],asset,dir_name,asset_name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment