Guest User

Untitled

a guest
Mar 27th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. from VTFWrapper.VTFLib import VTFLib
  2. import os
  3. from PIL import Image
  4. import struct
  5. import glob
  6. from ctypes import *
  7. import sys
  8. import argparse
  9.  
  10. ########################################
  11. # Section 1: Just validate them inputs #
  12. ########################################
  13.  
  14. parser = argparse.ArgumentParser(description='Process some integers.')
  15. parser.add_argument('--input', metavar='directory/*.png', type=str, help='')
  16. parser.add_argument('--output', metavar='O.vtf', help='sum the integers (default: find the max)')
  17.  
  18. args = parser.parse_args()
  19.  
  20. if args.input is None or args.output is None:
  21. print("You need to set both --input and --output")
  22. sys.exit(-1)
  23.  
  24. #########################################
  25. # Section 2: Setup of the vtflib things #
  26. #########################################
  27.  
  28. globby = glob.glob(args.input) # Here, we take the 'input' (which can use wildcards like *.png)..
  29. globby = sorted(globby) # And sort them.. :'). Glob returns them possibly randomly
  30.  
  31. vl = VTFLib() # Init the library
  32. print(vl.get_last_error()) # We sprinkle these all around :P
  33. options = vl.create_default_params_structure() # Set some settings
  34. options.ImageFormat = 13
  35. options.Mipmaps = 0
  36. #print(repr(options))
  37.  
  38. # This is the type.. wrangled.. for things. Ignore it. It'll be useful later :P
  39. LP_c_byte = POINTER(c_byte)
  40.  
  41. # Allright! So! We need to grab two things
  42. pointerlist = [] # 1) A list of memory pointers to the images we loaded...
  43. datalist = [] # 2) A python reference to them so python does not garbage collect it.
  44.  
  45.  
  46. ########################################################################
  47. # Section 3: Now! We need to grab the image data from the png files... #
  48. ########################################################################
  49.  
  50. i = 0
  51. for file in globby:
  52. im = Image.open(file) # Open the file
  53. bytebuffer = bytearray(b'') # Create somewhere to stick the data..
  54. for thebytes in list(im.getdata()): # From im.getdata, we get a list of bytes, ordered like [(0,0,0,1),...
  55. for individualbyte in thebytes: # so we loop through the 4 bytes for every pixel in that list
  56. bytebuffer.append(individualbyte) # and stick it in the buffer
  57.  
  58. safebuffer = bytes(bytebuffer) # After we are done, we need to do a quick conversion so all the bytes are linear in the memory
  59. datalist.append(safebuffer) # This is just to stop python from garbage collecting the data while vtflib is working, because it thinks they are no longer used
  60. pointerlist.append(cast(safebuffer, LP_c_byte)) # vtflib expects a list of memory pointers for each frame
  61. i = i + 1
  62.  
  63. ########################################################################
  64. # Section 4: Stick em right up vtflib's arse ... #
  65. ########################################################################
  66.  
  67. pointerlist_raw = (LP_c_byte * len(pointerlist))(*pointerlist) # ... we need to tell ctypes that the list of pointers is, in fact, a list of pointers. IÄ IÄ stackoverflow f'thgn
  68.  
  69. vl.image_create_multiple(512, 128, len(pointerlist_raw), 1, 1, pointerlist_raw, options) # Ya! Ya!
  70. print(vl.get_last_error())
  71.  
  72. print(vl.image_save(args.output)) # Finally, save that biatch
  73. print(vl.get_last_error())
Advertisement
Add Comment
Please, Sign In to add comment