Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import traceback
- import glob
- import json
- import os
- import subprocess
- import os.path
- import sys
- import requests
- import shutil
- from PIL import *
- from PIL import Image
- from PIL import ImageEnhance
- from PIL import ImageChops
- from StringIO import StringIO
- cdn_url = 'http://v2.cdn.android.brave.a-lim.jp'
- #cdn_url = 'http://static.bravefrontier.gumi-europe.net/content'
- class Point:
- def __init__(self):
- self.x = 0
- self.y = 0
- def reduce_opacity(im, opacity):
- """
- Returns an image with reduced opacity.
- Taken from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879
- """
- assert opacity >= 0 and opacity <= 1
- if im.mode != 'RGBA':
- im = im.convert('RGBA')
- else:
- im = im.copy()
- alpha = im.split()[3]
- alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
- im.putalpha(alpha)
- return im
- def blend(im):
- bands = im.split()
- alpha = bands[3]
- red = bands[0]
- blue = bands[1]
- green = bands[2]
- for x in range(0, im.size[0]):
- for y in range(0, im.size[1]):
- if alpha.getpixel((x, y)) == 0:
- continue
- pixval = ((red.getpixel((x, y)) + blue.getpixel((x, y)) + green.getpixel((x, y))) / 3)
- alpha.putpixel((x, y), pixval)
- im.putalpha(alpha)
- return imageop
- def download_files(id, cgg_file):
- downloaded = {}
- cgg_url = cdn_url + '/unit/cgg/' + cgg_file
- cgg_id = cgg_file.split('_')[2].split('.')[0]
- idle_cgs_url = cdn_url + '/unit/cgs/unit_idle_cgs_' + cgg_id + '.csv'
- move_cgs_url = cdn_url + '/unit/cgs/unit_move_cgs_' + cgg_id + '.csv'
- atk_cgs_url = cdn_url + '/unit/cgs/unit_atk_cgs_' + cgg_id + '.csv'
- anime_url = cdn_url + '/unit/img/unit_anime_' + id + '.png'
- print 'Downloading ' + id + ' cgg file.'
- downloaded.update({'cgg': requests.get(cgg_url).text})
- print 'Downloading ' + id + ' idle file.'
- downloaded.update({'idle': requests.get(idle_cgs_url).text})
- print 'Downloading ' + id + ' move file.'
- downloaded.update({'move': requests.get(move_cgs_url).text})
- print 'Downloading ' + id + ' atk file.'
- downloaded.update({'atk': requests.get(atk_cgs_url).text})
- print 'Downloading ' + id + ' anime file.'
- downloaded.update({'anime': Image.open(StringIO(requests.get(anime_url).content))})
- return downloaded
- def make_gif(data, cgs_file, output):
- cgs_lines = cgs_file.split('\n')
- gif_command = 'convert -dispose 2'
- apng_command = 'F:\Tools\\apngasm\\bin\\apngasm.exe --force -o jp_anim/' + output.replace('gif', 'png')
- temp_ind = 0;
- for line in cgs_lines:
- params = line.split(',')
- if len(params) < 2:
- break
- frame_index = int(params[0])
- delay = int(params[3])
- x_pos = int(params[1])
- y_pos = int(params[2])
- #offset = False
- img_name = 'temp/temp' + str(frame_index) + '.png'
- if x_pos != 0 or y_pos != 0:
- frame_img = Image.new('RGBA', (2000, 2000))
- for part in reversed(data['frames'][frame_index]):
- box = (part['img_x'], part['img_y'],
- part['img_x'] + part['img_width'],
- part['img_y'] + part['img_height'])
- crop = img.crop(box)
- if part['blend_mode'] == 1:
- print 'blend mode is not zero, attempting to blend'
- crop = blend(crop)
- if part['rotate'] != 0:
- print 'we rotating by ' + str(part['rotate'])
- crop = crop.rotate(360 - part['rotate'])
- if part['flipx']:
- print 'we flipping x'
- crop = crop.transpose(Image.FLIP_LEFT_RIGHT)
- if part['flipy']:
- print 'we flipping y'
- crop = crop.transpose(Image.FLIP_TOP_BOTTOM)
- if part['opacity'] != 100.0:
- print 'opacity not 1, reducing opacity'
- print part['opacity']
- #crop = reduce_opacity(crop, part['opacity'] / 100)
- frame_img.paste(crop,
- ((2000/2 + part['x_pos'] + x_pos),
- (2000/2 + part['y_pos'] + y_pos)),
- mask=crop)
- frame_img.save('temp/temp' + str(frame_index) + '_offset' + str(temp_ind) + '.png', 'PNG')
- img_name = 'temp/temp' + str(frame_index) + '_offset' + str(temp_ind) + '.png'
- temp_ind += 1
- #offset = True
- gif_command += ' -alpha set -delay ' \
- + str(delay) + 'x60 ' + img_name
- apng_command += ' ' + img_name + ' ' + str(delay) + ':60'
- gif_command += ' -loop 0 -alpha set -trim -layers TrimBounds jp_anim/' + output
- os.system(gif_command)
- os.system(apng_command)
- if __name__ == '__main__':
- unit_dir = 'server_dat/jp/legacy/Ver*_2r9cNSdt.*'
- #unit_dir = 'Ver*_2r9cNSdt.dat'
- with open(max(glob.iglob(unit_dir), key=os.path.getctime)) as f:
- units = json.load(f)
- for unit in units:
- try:
- unit_id = unit['pn16CNah']
- unit_series = unit['9PsmH7tz']
- if not os.path.exists('jp_anim\\' + unit_series):
- os.makedirs('jp_anim\\' + unit_series)
- file_check = 'jp_anim/unit_' + unit_id + '_idle.gif'
- file_check_2 = 'jp_anim/' + unit_series + '/unit_' + unit_id + '_idle.gif'
- if os.path.isfile(file_check):
- print 'Moving ' + unit_id + ' since it already exists in the wrong place.'
- try:
- shutil.move(file_check, file_check_2)
- except:
- pass
- try:
- shutil.move(file_check.replace('idle', 'move'), file_check_2.replace('idle', 'move'))
- except:
- pass
- try:
- shutil.move(file_check.replace('idle', 'atk'), file_check_2.replace('idle', 'atk'))
- except:
- pass
- try:
- shutil.move(file_check.replace('.gif', '.png'), file_check_2.replace('.gif', '.png'))
- except:
- pass
- try:
- shutil.move(file_check.replace('idle.gif', 'move.png'), file_check_2.replace('idle.gif', 'move.png'))
- except:
- pass
- try:
- shutil.move(file_check.replace('idle.gif', 'atk.png'), file_check_2.replace('idle.gif', 'atk.png'))
- except:
- pass
- continue
- if os.path.isfile(file_check_2):
- print 'Skipping ' + unit_id + ' since it already exists.'
- continue;
- cgg_unit_id = unit['QqfI9mM4'].split('_')[2].split('.')[0]
- files = download_files(unit_id, unit['QqfI9mM4'])
- data = dict()
- data['frames'] = []
- cgg_lines = files['cgg'].replace('\r', '').split('\n')
- for line in cgg_lines:
- params = line.split(',')
- if len(params) < 2:
- break
- anchor = int(params[0])
- count = int(params[1])
- index = 2
- parts = []
- for part_ind in range(0, count):
- part = dict()
- part['x_pos'] = int(params[index]) # *0.5
- part['y_pos'] = int(params[index + 1]) # *0.5
- part['next_type'] = int(params[index + 2])
- part['flipx'] = False
- part['flipy'] = False
- if part['next_type'] == 1:
- part['flipx'] = True
- if part['next_type'] == 2:
- part['flipy'] = True
- if part['next_type'] == 3:
- part['flipx'] = True
- part['flipy'] = True
- if part['next_type'] not in [0, 1, 2, 3]:
- raise Exception('WRONG TYPE: '
- + str(part['next_type']))
- part['blend_mode'] = int(params[index + 3])
- part['opacity'] = float(params[index + 4])
- part['rotate'] = int(params[index + 5])
- part['pageId'] = int(params[index + 10])
- part['img_x'] = int(params[index + 6]) # *0.5
- part['img_y'] = int(params[index + 7]) # *0.5
- part['img_width'] = int(params[index + 8]) # *0.5
- part['img_height'] = int(params[index + 9]) # *0.5
- parts.append(part)
- index = index + 11
- data['frames'].append(parts)
- #os.system('convert ' + anime_filepath + ' PNG32:'
- # + anime_filepath + '.temp.png')
- img = files['anime']
- if img.mode != 'RGBA':
- print 'img mode is ' + img.mode + ', converting to RGBA'
- img = img.convert('RGBA')
- for index in range(0, len(data['frames'])):
- frame_img = Image.new('RGBA', (2000, 2000))
- for part in reversed(data['frames'][index]):
- box = (part['img_x'], part['img_y'],
- part['img_x'] + part['img_width'],
- part['img_y'] + part['img_height'])
- crop = img.crop(box)
- if part['blend_mode'] == 1:
- print 'blend mode is not zero, attempting to blend'
- crop = blend(crop)
- if part['rotate'] != 0:
- print 'we rotating by ' + str(part['rotate'])
- crop = crop.rotate(360 - part['rotate'])
- if part['flipx']:
- print 'we flipping x'
- crop = crop.transpose(Image.FLIP_LEFT_RIGHT)
- if part['flipy']:
- print 'we flipping y'
- crop = crop.transpose(Image.FLIP_TOP_BOTTOM)
- if part['opacity'] != 100.0:
- print 'opacity not 1, reducing opacity'
- print part['opacity']
- #crop = reduce_opacity(crop, part['opacity'] / 100)
- frame_img.paste(crop,
- ((2000/2 + part['x_pos']),
- (2000/2 + part['y_pos'])),
- mask=crop)
- frame_img.save('temp/temp' + str(index)
- + '.png', 'PNG')
- print 'Making idle gif.'
- make_gif(data, files['idle'], unit_series + '\unit_' + unit_id + '_idle.gif')
- print 'Making move gif.'
- make_gif(data, files['move'], unit_series + '\unit_' + unit_id + '_move.gif')
- print 'Making atk gif.'
- make_gif(data, files['atk'], unit_series + '\unit_' + unit_id + '_atk.gif')
- # gif_command = 'convert -dispose 2'
- # with open(idle_cgs_filepath) as cgs:
- # cgs_lines = cgs.read().split('\n')
- # for line in cgs_lines:
- # params = line.split(',')
- # if len(params) < 2:
- # break
- # frame_index = int(params[0])
- # delay = int(params[3])
- # x_pos = int(params[1])
- # y_pos = int(params[2])
- # offset = False
- # if x_pos != 0 or y_pos != 0:
- # frame_img = Image.new('RGBA', (2000, 2000))
- # for part in reversed(data['frames'][index]):
- # box = (part['img_x'], part['img_y'],
- # part['img_x'] + part['img_width'],
- # part['img_y'] + part['img_height'])
- # crop = img.crop(box)
- # if part['flipx']:
- # print 'we flipping x'
- # crop.transpose(Image.FLIP_LEFT_RIGHT)
- # if part['flipy']:
- # print 'we flipping y'
- # crop.transpose(Image.FLIP_TOP_BOTTOM)
- # if part['rotate'] != 0:
- # print 'we rotating by ' + str(part['rotate'])
- # crop.rotate(part['rotate'])
- # frame_img.paste(crop,
- # ((2000/2 + part['x_pos'] + x_pos),
- # (2000/2 + part['y_pos'] + y_pos)),
- # mask=crop)
- # frame_img.save('frames/temp' + str(index)
- # + '_offset.png', 'PNG')
- # offset = True
- # gif_command += ' -alpha set -delay ' \
- # + str(delay) + 'x60 frames/temp' \
- # + str(frame_index) + ('.png' if not offset else '_offset.png')
- # gif_command += \
- # ' -loop 0 -alpha set -trim -layers TrimBounds anim/unit_ills_anime_' \
- # + unit_id + '.gif'
- # if proceed:
- # os.system(gif_command)
- # gif_command = 'convert -dispose 2'
- # move_cgs_filepath = ('files/' if not jp else 'jp_files/') + 'unit_move_cgs_' \
- # + cgs_id + '.csv'
- # proceed = True
- # if os.path.isfile(move_cgs_filepath):
- # with open(move_cgs_filepath) as cgs:
- # cgs_lines = cgs.read().split('\n')
- # for line in cgs_lines:
- # params = line.split(',')
- # if len(params) < 2:
- # break
- # frame_index = int(params[0])
- # delay = int(params[3])
- # x_pos = int(params[1])
- # y_pos = int(params[2])
- # if x_pos != 0 or y_pos != 0:
- # frame_img = Image.new('RGBA', (2000, 2000))
- # for part in reversed(data['frames'][frame_index]):
- # box = (part['img_x'], part['img_y'],
- # part['img_x'] + part['img_width'],
- # part['img_y'] + part['img_height'])
- # crop = img.crop(box)
- # if part['flipx']:
- # print 'we flipping x'
- # crop.transpose(Image.FLIP_LEFT_RIGHT)
- # if part['flipy']:
- # print 'we flipping y'
- # crop.transpose(Image.FLIP_TOP_BOTTOM)
- # if part['rotate'] != 0:
- # print 'we rotating by ' + str(part['rotate'])
- # crop.rotate(part['rotate'])
- # frame_img.paste(crop,
- # ((2000/2 + part['x_pos'] + x_pos),
- # (2000/2 + part['y_pos'] + y_pos)),
- # mask=crop)
- # frame_img.save('frames/temp' + str(frame_index)
- # + '_offset.png', 'PNG')
- # gif_command += ' -alpha set -delay ' \
- # + str(delay) + 'x60 frames/temp' \
- # + str(frame_index) + ('.png' if not offset else '_offset.png')
- # if jp:
- # gif_command += \
- # ' -loop 0 -alpha set -trim -layers TrimBounds jp_anim/unit_ills_anime_move_' \
- # + unit_id + '.gif'
- # else:
- # gif_command += \
- # ' -loop 0 -alpha set -trim -layers TrimBounds anim/unit_ills_anime_move_' \
- # + unit_id + '.gif'
- # if proceed:
- # os.system(gif_command)
- # gif_command = 'convert -dispose 2'
- # atk_cgs_filepath = ('files/' if not jp else 'jp_files/') + 'unit_atk_cgs_' \
- # + cgs_id + '.csv'
- # proceed = True
- # if os.path.isfile(atk_cgs_filepath):
- # with open(atk_cgs_filepath) as cgs:
- # cgs_lines = cgs.read().split('\n')
- # for line in cgs_lines:
- # params = line.split(',')
- # if len(params) < 2:
- # break
- # frame_index = int(params[0])
- # delay = int(params[3])
- # x_pos = int(params[1])
- # y_pos = int(params[2])
- # if x_pos != 0 or y_pos != 0:
- # frame_img = Image.new('RGBA', (2000, 2000))
- # for part in reversed(data['frames'][frame_index]):
- # box = (part['img_x'], part['img_y'],
- # part['img_x'] + part['img_width'],
- # part['img_y'] + part['img_height'])
- # crop = img.crop(box)
- # if part['flipx']:
- # print 'we flipping x'
- # crop.transpose(Image.FLIP_LEFT_RIGHT)
- # if part['flipy']:
- # print 'we flipping y'
- # crop.transpose(Image.FLIP_TOP_BOTTOM)
- # if part['rotate'] != 0:
- # print 'we rotating by ' + str(part['rotate'])
- # crop.rotate(part['rotate'])
- # frame_img.paste(crop,
- # ((2000/2 + part['x_pos'] + x_pos),
- # (2000/2 + part['y_pos'] + y_pos)),
- # mask=crop)
- # frame_img.save('frames/temp' + str(frame_index)
- # + '_offset.png', 'PNG')
- # gif_command += ' -alpha set -delay ' \
- # + str(delay) + 'x60 frames/temp' \
- # + str(frame_index) + ('.png' if not offset else '_offset.png')
- # gif_command += \
- # ' -loop 0 -alpha set -trim -layers TrimBounds anim/unit_ills_anime_attack_' \
- # + unit_id + '.gif'
- # if proceed:
- # os.system(gif_command)
- # os.remove(anime_filepath + '.temp.png')
- except KeyboardInterrupt:
- raise
- except:
- print 'failed to parse unit'
- print traceback.format_exc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement