转自: http://blog..net/offbye/article/details/50012605
Android屏幕分辨率适配的图标处理比较麻烦,让UI做不同尺寸的图片也挺浪费时间的,并且容易出错,于是用Python写了个工具自动化处理图片,UI只需要做好1080*1920分辨率下的图片就可以了,其它分辨率的图片自动生成。
[python] view
plaincopy
importos.path
importsys
fromPILimportImage
"""
自动生成不同分辨率下的App图片
UI设计1080*1920分辨率图片,放在drawable-xxhdpi目录下,自动生成其它的分辨率图片
"""
__author__ = ['"Xitao":']
defimage_resize(img_file, target, percent):
"""resize image and save to target path
:param img_file: image file path
:param target: save path
:param percent: resize percent
:return:
"""
img = Image.open(img_file)
print(img.size)
width, height = img.size
target_img = img.resize((int(width * percent), int(height * percent)), Image.ANTIALIAS)
target_img.save(target)
img.close()
target_img.close()
print(" save target image to "+ target)
defpath_resize(src, target, percent):
ifnotos.path.isdir(src):
print(src +" must be a dir")
return-1
os.chdir(src)
cwd = os.getcwd()
dirs = os.listdir(cwd)
forfile_nameindirs:
printfile_name
iffile_name.endswith('.9.png'):
continue
src_file = os.path.join(cwd, file_name)
ifnotos.path.exists(target):
os.mkdir(target)
image_resize(src_file, target + '/'+ file_name, percent)
defandroid(res_dir):
xxhdpi_path = res_dir + "/drawable-xxhdpi/"
ifnotos.path.isdir(xxhdpi_path):
print("xxhdpi_path must be a dir")
return-1
path_resize(xxhdpi_path, res_dir + '/drawable-xhdpi',0.667)
path_resize(xxhdpi_path, res_dir + '/drawable-hdpi',0.444)
path_resize(xxhdpi_path, res_dir + '/drawable-mdpi',0.296)
if__name__ =="__main__":
print('please input your androd res dir path')
print(sys.argv)
ifsys.argv[1]:
android(sys.argv[1])