Last active
August 29, 2015 14:06
-
-
Save nowsprinting/6b245d144d112e3ad548 to your computer and use it in GitHub Desktop.
drawable-xhdpiディレクトリにある画像ファイルを元に、(縦横二倍して)drawable-xxxhdpiを生成する
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
#!/usr/bin/env python2.7 | |
#coding=utf-8 | |
""" | |
指定ディレクトリ下のdrawable-xhdpiディレクトリにある画像ファイルを元に、(縦横二倍して)drawable-xxxhdpiディレクトリ下に画像ファイルを生成します。 | |
主にAndroid用。 | |
Usage | |
$ create_xxxhdpi_images_from_xhdpi.py -r <RESOURCE_DIR> | |
before | |
- <RESOURCE_DIR> | |
- drawable-xhdpi | |
- xxx.png | |
- yyy.png | |
after | |
- <RESOURCE_DIR> | |
- drawable-xhdpi | |
- xxx.png | |
- yyy.png | |
- drawable-xxxhdpi | |
- xxx.png | |
- yyy.png | |
This script required install PIL(Python Image Library) | |
$ sudo easy_install PIL | |
""" | |
__author__ = "Koji Hasegawa" | |
__copyright__ = "Copyright 2014, Koji Hasegawa" | |
__credits__ = ["Koji Hasegawa"] | |
__version__ = "1.0" | |
import shutil | |
import os | |
import re | |
import argparse | |
import sys | |
import Image | |
SOURCE_DIR = 'drawable-xhdpi' | |
DESTINATION_DIR = 'drawable-xxxhdpi' | |
if __name__ == '__main__': | |
# check arguments | |
argParser = argparse.ArgumentParser(description='Create xxxhdpi size image file script') | |
argParser.add_argument('-r', '--res', metavar='RESOURCE_DIR', required=True, help='Android resource directory') | |
args = argParser.parse_args() | |
# check source directory | |
src_dir = os.path.join(args.res, SOURCE_DIR); | |
if not os.path.exists(src_dir): | |
sys.exit("Not exists resource directory") | |
# create destination directory | |
dst_dir = os.path.join(args.res, DESTINATION_DIR); | |
if not os.path.exists(dst_dir): | |
try: | |
os.mkdir(dst_dir) | |
except OSError as err: | |
sys.exit("Can not create directory {0}. reason:{1}".format(dst_dir, err)) | |
# copy and resize | |
for filename in os.listdir(src_dir): | |
if filename!='.DS_Store': | |
src_image = os.path.join(src_dir, filename) | |
dst_image = os.path.join(dst_dir, filename) | |
# check distination file | |
if os.path.exists(dst_image): | |
is_exist = True | |
else: | |
is_exist = False | |
# copy and resize image | |
try: | |
img = Image.open(src_image) | |
dst_image_size = (img.size[0]*2, img.size[1]*2) | |
img.resize(dst_image_size, Image.ANTIALIAS).save(dst_image) | |
del img | |
except OSError as err: | |
sys.exit("Can not resize {0}. reason:{1}".format(filename, err)) | |
# report | |
if is_exist: | |
print "\033[32m- overwrite: {0}\033[m".format(filename) | |
else: | |
print "\033[32m- create: {0}\033[m".format(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment