74 lines
1.9 KiB
Python
74 lines
1.9 KiB
Python
#!python
|
|
import os, sys
|
|
import json
|
|
from PIL import Image
|
|
|
|
|
|
def convertImage( fileName ):
|
|
findIndex = fileName.rfind( '.' )
|
|
pngName = fileName[0:findIndex] + ".png"
|
|
|
|
imageVal = Image.open(fileName)
|
|
imageVal.save( pngName )
|
|
imageVal.close()
|
|
|
|
os.remove( fileName )
|
|
|
|
fileMetaPath = fileName + ".meta"
|
|
if os.path.exists( fileMetaPath ):
|
|
os.rename( fileMetaPath, pngName + ".meta" )
|
|
|
|
|
|
def run(inFolder,outFolder):
|
|
if not os.path.isdir(outFolder):
|
|
os.mkdir(outFolder)
|
|
|
|
schemePath = os.path.join(inFolder, 'Contents.json')
|
|
iconPath = os.path.join(inFolder, 'icon.png')
|
|
|
|
try:
|
|
org_image = Image.open(iconPath)
|
|
except FileNotFoundError:
|
|
print('missing icon file:' + iconPath)
|
|
return
|
|
|
|
try:
|
|
f = open( schemePath, 'r', encoding='utf-8' )
|
|
jsonString = f.read()
|
|
if 0 < len(jsonString):
|
|
jsonRoot = json.loads( jsonString )
|
|
f.close()
|
|
except FileNotFoundError:
|
|
print( 'missing scheme file:' + schemePath )
|
|
return
|
|
|
|
frameList = jsonRoot.get("images")
|
|
|
|
|
|
for frameData in frameList:
|
|
|
|
itemName = frameData.get("filename")
|
|
sizeVal = frameData.get("size")
|
|
strScale = frameData.get("scale")
|
|
|
|
indexSize = sizeVal.find( 'x' )
|
|
if -1 != indexSize:
|
|
widthVal = float( sizeVal[0:indexSize] )
|
|
heightVal = float( sizeVal[indexSize+1:])
|
|
else:
|
|
widthVal = heightVal = int( sizeVal )
|
|
|
|
indexSize = strScale.find( 'x' )
|
|
if -1 != indexSize:
|
|
scaleVal = float( strScale[0:indexSize] )
|
|
else:
|
|
scaleVal = float( strScale )
|
|
|
|
curSize = ( round(widthVal * scaleVal), round(heightVal * scaleVal) )
|
|
curImage = org_image.resize( curSize, 5 ) #Resampling.HAMMING
|
|
curImage.save( os.path.join(outFolder, itemName ) )
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run('in','out')
|