Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from VTFWrapper.VTFLib import VTFLib
- import os
- from PIL import Image
- import struct
- import glob
- from ctypes import *
- import sys
- import argparse
- ########################################
- # Section 1: Just validate them inputs #
- ########################################
- parser = argparse.ArgumentParser(description='Process some integers.')
- parser.add_argument('--input', metavar='directory/*.png', type=str, help='')
- parser.add_argument('--output', metavar='O.vtf', help='sum the integers (default: find the max)')
- args = parser.parse_args()
- if args.input is None or args.output is None:
- print("You need to set both --input and --output")
- sys.exit(-1)
- #########################################
- # Section 2: Setup of the vtflib things #
- #########################################
- globby = glob.glob(args.input) # Here, we take the 'input' (which can use wildcards like *.png)..
- globby = sorted(globby) # And sort them.. :'). Glob returns them possibly randomly
- vl = VTFLib() # Init the library
- print(vl.get_last_error()) # We sprinkle these all around :P
- options = vl.create_default_params_structure() # Set some settings
- options.ImageFormat = 13
- options.Mipmaps = 0
- #print(repr(options))
- # This is the type.. wrangled.. for things. Ignore it. It'll be useful later :P
- LP_c_byte = POINTER(c_byte)
- # Allright! So! We need to grab two things
- pointerlist = [] # 1) A list of memory pointers to the images we loaded...
- datalist = [] # 2) A python reference to them so python does not garbage collect it.
- ########################################################################
- # Section 3: Now! We need to grab the image data from the png files... #
- ########################################################################
- i = 0
- for file in globby:
- im = Image.open(file) # Open the file
- bytebuffer = bytearray(b'') # Create somewhere to stick the data..
- for thebytes in list(im.getdata()): # From im.getdata, we get a list of bytes, ordered like [(0,0,0,1),...
- for individualbyte in thebytes: # so we loop through the 4 bytes for every pixel in that list
- bytebuffer.append(individualbyte) # and stick it in the buffer
- safebuffer = bytes(bytebuffer) # After we are done, we need to do a quick conversion so all the bytes are linear in the memory
- 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
- pointerlist.append(cast(safebuffer, LP_c_byte)) # vtflib expects a list of memory pointers for each frame
- i = i + 1
- ########################################################################
- # Section 4: Stick em right up vtflib's arse ... #
- ########################################################################
- 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
- vl.image_create_multiple(512, 128, len(pointerlist_raw), 1, 1, pointerlist_raw, options) # Ya! Ya!
- print(vl.get_last_error())
- print(vl.image_save(args.output)) # Finally, save that biatch
- print(vl.get_last_error())
Advertisement
Add Comment
Please, Sign In to add comment