Advertisement
Nazta

Untitled

Sep 27th, 2015
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 20.51 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import traceback
  4. import glob
  5. import json
  6. import os
  7. import subprocess
  8. import os.path
  9. import sys
  10. import requests
  11. import shutil
  12. from PIL import *
  13. from PIL import Image
  14. from PIL import ImageEnhance
  15. from PIL import ImageChops
  16. from StringIO import StringIO
  17.  
  18. cdn_url = 'http://v2.cdn.android.brave.a-lim.jp'
  19. #cdn_url = 'http://static.bravefrontier.gumi-europe.net/content'
  20.  
  21. class Point:
  22.     def __init__(self):
  23.         self.x = 0
  24.         self.y = 0
  25.  
  26.  
  27. def reduce_opacity(im, opacity):
  28.     """
  29.    Returns an image with reduced opacity.
  30.    Taken from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879
  31.    """
  32.     assert opacity >= 0 and opacity <= 1
  33.     if im.mode != 'RGBA':
  34.         im = im.convert('RGBA')
  35.     else:
  36.         im = im.copy()
  37.     alpha = im.split()[3]
  38.     alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
  39.     im.putalpha(alpha)
  40.     return im
  41.  
  42.  
  43. def blend(im):
  44.     bands = im.split()
  45.     alpha = bands[3]
  46.     red = bands[0]
  47.     blue = bands[1]
  48.     green = bands[2]
  49.     for x in range(0, im.size[0]):
  50.         for y in range(0, im.size[1]):
  51.             if alpha.getpixel((x, y)) == 0:
  52.                 continue
  53.             pixval = ((red.getpixel((x, y)) + blue.getpixel((x, y)) + green.getpixel((x, y))) / 3)
  54.             alpha.putpixel((x, y), pixval)
  55.     im.putalpha(alpha)
  56.     return imageop
  57.  
  58.  
  59. def download_files(id, cgg_file):
  60.   downloaded = {}
  61.   cgg_url = cdn_url + '/unit/cgg/' + cgg_file
  62.   cgg_id = cgg_file.split('_')[2].split('.')[0]
  63.   idle_cgs_url = cdn_url + '/unit/cgs/unit_idle_cgs_' + cgg_id + '.csv'
  64.   move_cgs_url = cdn_url + '/unit/cgs/unit_move_cgs_' + cgg_id + '.csv'
  65.   atk_cgs_url = cdn_url + '/unit/cgs/unit_atk_cgs_' + cgg_id + '.csv'
  66.   anime_url = cdn_url + '/unit/img/unit_anime_' + id + '.png'
  67.   print 'Downloading ' + id + ' cgg file.'
  68.   downloaded.update({'cgg': requests.get(cgg_url).text})
  69.   print 'Downloading ' + id + ' idle file.'
  70.   downloaded.update({'idle': requests.get(idle_cgs_url).text})
  71.   print 'Downloading ' + id + ' move file.'
  72.   downloaded.update({'move': requests.get(move_cgs_url).text})
  73.   print 'Downloading ' + id + ' atk file.'
  74.   downloaded.update({'atk': requests.get(atk_cgs_url).text})
  75.   print 'Downloading ' + id + ' anime file.'
  76.   downloaded.update({'anime': Image.open(StringIO(requests.get(anime_url).content))})
  77.   return downloaded
  78.  
  79.  
  80. def make_gif(data, cgs_file, output):
  81.     cgs_lines = cgs_file.split('\n')
  82.     gif_command = 'convert -dispose 2'
  83.     apng_command = 'F:\Tools\\apngasm\\bin\\apngasm.exe --force -o jp_anim/' + output.replace('gif', 'png')
  84.     temp_ind = 0;
  85.     for line in cgs_lines:
  86.         params = line.split(',')
  87.         if len(params) < 2:
  88.             break
  89.         frame_index = int(params[0])
  90.         delay = int(params[3])
  91.         x_pos = int(params[1])
  92.         y_pos = int(params[2])
  93.         #offset = False
  94.         img_name = 'temp/temp' + str(frame_index) + '.png'
  95.         if x_pos != 0 or y_pos != 0:
  96.             frame_img = Image.new('RGBA', (2000, 2000))
  97.             for part in reversed(data['frames'][frame_index]):
  98.                 box = (part['img_x'], part['img_y'],
  99.                        part['img_x'] + part['img_width'],
  100.                        part['img_y'] + part['img_height'])
  101.                 crop = img.crop(box)
  102.                 if part['blend_mode'] == 1:
  103.                     print 'blend mode is not zero, attempting to blend'
  104.                     crop = blend(crop)
  105.                 if part['rotate'] != 0:
  106.                     print 'we rotating by ' + str(part['rotate'])
  107.                     crop = crop.rotate(360 - part['rotate'])
  108.                 if part['flipx']:
  109.                     print 'we flipping x'
  110.                     crop = crop.transpose(Image.FLIP_LEFT_RIGHT)
  111.                 if part['flipy']:
  112.                     print 'we flipping y'
  113.                     crop = crop.transpose(Image.FLIP_TOP_BOTTOM)
  114.                 if part['opacity'] != 100.0:
  115.                     print 'opacity not 1, reducing opacity'
  116.                     print part['opacity']
  117.                     #crop = reduce_opacity(crop, part['opacity'] / 100)
  118.                 frame_img.paste(crop,
  119.                                 ((2000/2 + part['x_pos'] + x_pos),
  120.                                 (2000/2 + part['y_pos'] + y_pos)),
  121.                                 mask=crop)
  122.             frame_img.save('temp/temp' + str(frame_index) + '_offset' + str(temp_ind) + '.png', 'PNG')
  123.             img_name = 'temp/temp' + str(frame_index) + '_offset' + str(temp_ind) + '.png'
  124.             temp_ind += 1
  125.             #offset = True
  126.         gif_command += ' -alpha set -delay ' \
  127.             + str(delay) + 'x60 ' + img_name
  128.         apng_command += ' ' + img_name + ' ' + str(delay) + ':60'
  129.     gif_command += ' -loop 0 -alpha set -trim -layers TrimBounds jp_anim/' + output
  130.     os.system(gif_command)
  131.     os.system(apng_command)
  132.  
  133.  
  134. if __name__ == '__main__':
  135.     unit_dir = 'server_dat/jp/legacy/Ver*_2r9cNSdt.*'
  136.     #unit_dir = 'Ver*_2r9cNSdt.dat'
  137.     with open(max(glob.iglob(unit_dir), key=os.path.getctime)) as f:
  138.         units = json.load(f)
  139.         for unit in units:
  140.             try:
  141.                 unit_id = unit['pn16CNah']
  142.                 unit_series = unit['9PsmH7tz']
  143.  
  144.                 if not os.path.exists('jp_anim\\' + unit_series):
  145.                     os.makedirs('jp_anim\\' + unit_series)
  146.  
  147.                 file_check = 'jp_anim/unit_' + unit_id + '_idle.gif'
  148.                 file_check_2 = 'jp_anim/' + unit_series + '/unit_' + unit_id + '_idle.gif'
  149.                 if os.path.isfile(file_check):
  150.                     print 'Moving ' + unit_id + ' since it already exists in the wrong place.'
  151.                     try:
  152.                         shutil.move(file_check, file_check_2)
  153.                     except:
  154.                         pass
  155.                     try:
  156.                         shutil.move(file_check.replace('idle', 'move'), file_check_2.replace('idle', 'move'))
  157.                     except:
  158.                         pass
  159.                     try:
  160.                         shutil.move(file_check.replace('idle', 'atk'), file_check_2.replace('idle', 'atk'))
  161.                     except:
  162.                         pass
  163.                     try:
  164.                         shutil.move(file_check.replace('.gif', '.png'), file_check_2.replace('.gif', '.png'))
  165.                     except:
  166.                         pass
  167.                     try:
  168.                         shutil.move(file_check.replace('idle.gif', 'move.png'), file_check_2.replace('idle.gif', 'move.png'))
  169.                     except:
  170.                         pass
  171.                     try:
  172.                         shutil.move(file_check.replace('idle.gif', 'atk.png'), file_check_2.replace('idle.gif', 'atk.png'))
  173.                     except:
  174.                         pass
  175.                     continue
  176.                 if os.path.isfile(file_check_2):
  177.                     print 'Skipping ' + unit_id + ' since it already exists.'
  178.                     continue;
  179.  
  180.                 cgg_unit_id = unit['QqfI9mM4'].split('_')[2].split('.')[0]
  181.  
  182.                 files = download_files(unit_id, unit['QqfI9mM4'])
  183.  
  184.                 data = dict()
  185.                 data['frames'] = []
  186.  
  187.                 cgg_lines = files['cgg'].replace('\r', '').split('\n')
  188.                 for line in cgg_lines:
  189.                     params = line.split(',')
  190.                     if len(params) < 2:
  191.                         break
  192.                     anchor = int(params[0])
  193.                     count = int(params[1])
  194.                     index = 2
  195.                     parts = []
  196.                     for part_ind in range(0, count):
  197.                         part = dict()
  198.                         part['x_pos'] = int(params[index])  # *0.5
  199.                         part['y_pos'] = int(params[index + 1])  # *0.5
  200.                         part['next_type'] = int(params[index + 2])
  201.                         part['flipx'] = False
  202.                         part['flipy'] = False
  203.                         if part['next_type'] == 1:
  204.                             part['flipx'] = True
  205.                         if part['next_type'] == 2:
  206.                             part['flipy'] = True
  207.                         if part['next_type'] == 3:
  208.                             part['flipx'] = True
  209.                             part['flipy'] = True
  210.                         if part['next_type'] not in [0, 1, 2, 3]:
  211.                             raise Exception('WRONG TYPE: '
  212.                                     + str(part['next_type']))
  213.                         part['blend_mode'] = int(params[index + 3])
  214.                         part['opacity'] = float(params[index + 4])
  215.                         part['rotate'] = int(params[index + 5])
  216.                         part['pageId'] = int(params[index + 10])
  217.                         part['img_x'] = int(params[index + 6])  # *0.5
  218.                         part['img_y'] = int(params[index + 7])  # *0.5
  219.                         part['img_width'] = int(params[index + 8])  # *0.5
  220.                         part['img_height'] = int(params[index + 9])  # *0.5
  221.                         parts.append(part)
  222.                         index = index + 11
  223.                     data['frames'].append(parts)
  224.  
  225.                 #os.system('convert ' + anime_filepath + ' PNG32:'
  226.                 #          + anime_filepath + '.temp.png')
  227.  
  228.                 img = files['anime']
  229.                 if img.mode != 'RGBA':
  230.                     print 'img mode is ' + img.mode + ', converting to RGBA'
  231.                     img = img.convert('RGBA')
  232.                 for index in range(0, len(data['frames'])):
  233.                     frame_img = Image.new('RGBA', (2000, 2000))
  234.                     for part in reversed(data['frames'][index]):
  235.                         box = (part['img_x'], part['img_y'],
  236.                                part['img_x'] + part['img_width'],
  237.                                part['img_y'] + part['img_height'])
  238.                         crop = img.crop(box)
  239.                         if part['blend_mode'] == 1:
  240.                             print 'blend mode is not zero, attempting to blend'
  241.                             crop = blend(crop)
  242.                         if part['rotate'] != 0:
  243.                             print 'we rotating by ' + str(part['rotate'])
  244.                             crop = crop.rotate(360 - part['rotate'])
  245.                         if part['flipx']:
  246.                             print 'we flipping x'
  247.                             crop = crop.transpose(Image.FLIP_LEFT_RIGHT)
  248.                         if part['flipy']:
  249.                             print 'we flipping y'
  250.                             crop = crop.transpose(Image.FLIP_TOP_BOTTOM)
  251.                         if part['opacity'] != 100.0:
  252.                             print 'opacity not 1, reducing opacity'
  253.                             print part['opacity']
  254.                             #crop = reduce_opacity(crop, part['opacity'] / 100)
  255.                         frame_img.paste(crop,
  256.                                         ((2000/2 + part['x_pos']),
  257.                                         (2000/2 + part['y_pos'])),
  258.                                         mask=crop)
  259.                     frame_img.save('temp/temp' + str(index)
  260.                             + '.png', 'PNG')
  261.  
  262.                 print 'Making idle gif.'
  263.                 make_gif(data, files['idle'], unit_series + '\unit_' + unit_id + '_idle.gif')
  264.                 print 'Making move gif.'
  265.                 make_gif(data, files['move'], unit_series + '\unit_' + unit_id + '_move.gif')
  266.                 print 'Making atk gif.'
  267.                 make_gif(data, files['atk'], unit_series + '\unit_' + unit_id + '_atk.gif')
  268.  
  269.                 # gif_command = 'convert -dispose 2'
  270.                 # with open(idle_cgs_filepath) as cgs:
  271.                 #     cgs_lines = cgs.read().split('\n')
  272.                 #     for line in cgs_lines:
  273.                 #         params = line.split(',')
  274.                 #         if len(params) < 2:
  275.                 #             break
  276.                 #         frame_index = int(params[0])
  277.                 #         delay = int(params[3])
  278.                 #         x_pos = int(params[1])
  279.                 #         y_pos = int(params[2])
  280.                 #         offset = False
  281.                 #         if x_pos != 0 or y_pos != 0:
  282.                 #             frame_img = Image.new('RGBA', (2000, 2000))
  283.                 #             for part in reversed(data['frames'][index]):
  284.                 #                 box = (part['img_x'], part['img_y'],
  285.                 #                        part['img_x'] + part['img_width'],
  286.                 #                        part['img_y'] + part['img_height'])
  287.                 #                 crop = img.crop(box)
  288.                 #                 if part['flipx']:
  289.                 #                     print 'we flipping x'
  290.                 #                     crop.transpose(Image.FLIP_LEFT_RIGHT)
  291.                 #                 if part['flipy']:
  292.                 #                     print 'we flipping y'
  293.                 #                     crop.transpose(Image.FLIP_TOP_BOTTOM)
  294.                 #                 if part['rotate'] != 0:
  295.                 #                     print 'we rotating by ' + str(part['rotate'])
  296.                 #                     crop.rotate(part['rotate'])
  297.  
  298.                 #                 frame_img.paste(crop,
  299.                 #                                 ((2000/2 + part['x_pos'] + x_pos),
  300.                 #                                 (2000/2 + part['y_pos'] + y_pos)),
  301.                 #                                 mask=crop)
  302.                 #             frame_img.save('frames/temp' + str(index)
  303.                 #                     + '_offset.png', 'PNG')
  304.                 #             offset = True
  305.                 #         gif_command += ' -alpha set -delay ' \
  306.                 #             + str(delay) + 'x60 frames/temp' \
  307.                 #             + str(frame_index) + ('.png' if not offset else '_offset.png')
  308.                 #     gif_command += \
  309.                 #         ' -loop 0 -alpha set -trim -layers TrimBounds anim/unit_ills_anime_' \
  310.                 #         + unit_id + '.gif'
  311.                 #     if proceed:
  312.                 #         os.system(gif_command)
  313.  
  314.                 # gif_command = 'convert -dispose 2'
  315.                 # move_cgs_filepath = ('files/' if not jp else 'jp_files/') + 'unit_move_cgs_' \
  316.                 #     + cgs_id + '.csv'
  317.                 # proceed = True
  318.                 # if os.path.isfile(move_cgs_filepath):
  319.                 #     with open(move_cgs_filepath) as cgs:
  320.                 #         cgs_lines = cgs.read().split('\n')
  321.                 #         for line in cgs_lines:
  322.                 #             params = line.split(',')
  323.                 #             if len(params) < 2:
  324.                 #                 break
  325.                 #             frame_index = int(params[0])
  326.                 #             delay = int(params[3])
  327.                 #             x_pos = int(params[1])
  328.                 #             y_pos = int(params[2])
  329.                 #             if x_pos != 0 or y_pos != 0:
  330.                 #                 frame_img = Image.new('RGBA', (2000, 2000))
  331.                 #                 for part in reversed(data['frames'][frame_index]):
  332.                 #                     box = (part['img_x'], part['img_y'],
  333.                 #                            part['img_x'] + part['img_width'],
  334.                 #                            part['img_y'] + part['img_height'])
  335.                 #                     crop = img.crop(box)
  336.                 #                     if part['flipx']:
  337.                 #                         print 'we flipping x'
  338.                 #                         crop.transpose(Image.FLIP_LEFT_RIGHT)
  339.                 #                     if part['flipy']:
  340.                 #                         print 'we flipping y'
  341.                 #                         crop.transpose(Image.FLIP_TOP_BOTTOM)
  342.                 #                     if part['rotate'] != 0:
  343.                 #                         print 'we rotating by ' + str(part['rotate'])
  344.                 #                         crop.rotate(part['rotate'])
  345.  
  346.                 #                     frame_img.paste(crop,
  347.                 #                                     ((2000/2 + part['x_pos'] + x_pos),
  348.                 #                                     (2000/2 + part['y_pos'] + y_pos)),
  349.                 #                                     mask=crop)
  350.                 #                 frame_img.save('frames/temp' + str(frame_index)
  351.                 #                         + '_offset.png', 'PNG')
  352.                 #             gif_command += ' -alpha set -delay ' \
  353.                 #                 + str(delay) + 'x60 frames/temp' \
  354.                 #                 + str(frame_index) + ('.png' if not offset else '_offset.png')
  355.                 #         if jp:
  356.                 #             gif_command += \
  357.                 #                 ' -loop 0 -alpha set -trim -layers TrimBounds jp_anim/unit_ills_anime_move_' \
  358.                 #                 + unit_id + '.gif'
  359.                 #         else:
  360.                 #             gif_command += \
  361.                 #                 ' -loop 0 -alpha set -trim -layers TrimBounds anim/unit_ills_anime_move_' \
  362.                 #                 + unit_id + '.gif'
  363.                 #         if proceed:
  364.                 #             os.system(gif_command)
  365.  
  366.                 # gif_command = 'convert -dispose 2'
  367.                 # atk_cgs_filepath = ('files/' if not jp else 'jp_files/') + 'unit_atk_cgs_' \
  368.                 #     + cgs_id + '.csv'
  369.                 # proceed = True
  370.                 # if os.path.isfile(atk_cgs_filepath):
  371.                 #     with open(atk_cgs_filepath) as cgs:
  372.                 #         cgs_lines = cgs.read().split('\n')
  373.                 #         for line in cgs_lines:
  374.                 #             params = line.split(',')
  375.                 #             if len(params) < 2:
  376.                 #                 break
  377.                 #             frame_index = int(params[0])
  378.                 #             delay = int(params[3])
  379.                 #             x_pos = int(params[1])
  380.                 #             y_pos = int(params[2])
  381.                 #             if x_pos != 0 or y_pos != 0:
  382.                 #                 frame_img = Image.new('RGBA', (2000, 2000))
  383.                 #                 for part in reversed(data['frames'][frame_index]):
  384.                 #                     box = (part['img_x'], part['img_y'],
  385.                 #                            part['img_x'] + part['img_width'],
  386.                 #                            part['img_y'] + part['img_height'])
  387.                 #                     crop = img.crop(box)
  388.                 #                     if part['flipx']:
  389.                 #                         print 'we flipping x'
  390.                 #                         crop.transpose(Image.FLIP_LEFT_RIGHT)
  391.                 #                     if part['flipy']:
  392.                 #                         print 'we flipping y'
  393.                 #                         crop.transpose(Image.FLIP_TOP_BOTTOM)
  394.                 #                     if part['rotate'] != 0:
  395.                 #                         print 'we rotating by ' + str(part['rotate'])
  396.                 #                         crop.rotate(part['rotate'])
  397.  
  398.                 #                     frame_img.paste(crop,
  399.                 #                                     ((2000/2 + part['x_pos'] + x_pos),
  400.                 #                                     (2000/2 + part['y_pos'] + y_pos)),
  401.                 #                                     mask=crop)
  402.                 #                 frame_img.save('frames/temp' + str(frame_index)
  403.                 #                         + '_offset.png', 'PNG')
  404.                 #             gif_command += ' -alpha set -delay ' \
  405.                 #                 + str(delay) + 'x60 frames/temp' \
  406.                 #                 + str(frame_index) + ('.png' if not offset else '_offset.png')
  407.                 #         gif_command += \
  408.                 #             ' -loop 0 -alpha set -trim -layers TrimBounds anim/unit_ills_anime_attack_' \
  409.                 #             + unit_id + '.gif'
  410.                 #         if proceed:
  411.                 #             os.system(gif_command)
  412.  
  413.                 # os.remove(anime_filepath + '.temp.png')
  414.             except KeyboardInterrupt:
  415.                 raise
  416.             except:
  417.                 print 'failed to parse unit'
  418.                 print traceback.format_exc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement