Skip to content

Instantly share code, notes, and snippets.

@dewyze
Created January 9, 2016 19:58
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 dewyze/391d09c12f3b8f76a9fa to your computer and use it in GitHub Desktop.
Save dewyze/391d09c12f3b8f76a9fa to your computer and use it in GitHub Desktop.
AndroidPicResize.py
import os
import sys
from PIL import Image
"""
Version 0.1.0
(c) 2013 John DeWyze
License: MIT
See README.md for usage guidelines
"""
def main(argv=sys.argv):
SCALES = {'drawable-xhdpi': 2.0/3.0, 'drawable-hdpi': .5, 'drawable-mdpi': 1.0/3.0}
FOLDERS = ["drawable-xhdpi", "drawable-hdpi", "drawable-mdpi"]
ACCEPTED_EXTENSIONS = ['.png', '.jpg', '.gif']
if sys.argv[1] == None:
directory = os.getcwd()
else:
directory = sys.argv[1]
root_path = os.path.join(directory,"drawable-xxhdpi")
if not(os.path.exists(root_path) and os.path.isdir(root_path)):
print("No xxhdpi folder exists, exiting")
sys.exit()
for folder in FOLDERS:
if folder in os.listdir() and os.path.isdir(folder):
print("%s folder exists" % folder)
else:
print("Creating folder:", folder)
os.makedirs(folder)
for obj in os.listdir(root_path):
if obj[-4:] not in ACCEPTED_EXTENSIONS:
break
for folder in FOLDERS:
img = Image.open(os.path.join(root_path, obj))
new_path = os.path.join(directory,folder,obj)
size = tuple([int(SCALES.get(folder)*x) for x in img.size])
img.thumbnail(size, Image.ANTIALIAS)
img.save(new_path)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment