Advertisement
Guest User

txtconvert.py

a guest
Jul 30th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. import sys
  2. import os
  3. from PIL import Image
  4. import numpy as np
  5.  
  6. f = open(sys.argv[1], 'r')
  7. TD = None
  8. textures_raw = {}
  9. blklist = ['=','{','}','[',']']
  10. for x in f:
  11.     if TD is not None:
  12.         rn = x.replace(' ','').strip().split(',')
  13.         vals = []
  14.         for n in rn:
  15.             text = n[2:]
  16.             val = []
  17.             if text:
  18.                 tmp = bin(int(text, 16))[2:].zfill(16)
  19.                 alpha = tmp[-1]
  20.                 rgb = tmp[:-1]
  21.                 chunks =  [rgb[5*i:5*(i+1)] for i in xrange(len(rgb)//5)]
  22.                 val = [(int(x, 2) * 255 / 31) for x in chunks]
  23.                 val.append(int(alpha)*255)
  24.                 vals.append(tuple(val))
  25.         if vals:
  26.             textures_raw[TD].append(vals)
  27.         if x.find('};') >= 0:
  28.             TD = None
  29.     if x.find('static unsigned short') >= 0:
  30.         name = x.replace('static unsigned short','')
  31.         name = ''.join([c for c in name if c not in blklist])
  32.         print(name)
  33.         TD = name.strip()
  34.         textures_raw[TD] = []
  35.  
  36. for tx in textures_raw:
  37.     array = np.array(textures_raw[tx], dtype=np.uint8)
  38.     img = Image.fromarray(array, 'RGBA')
  39.     img.save(tx+'.png')
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement